使用刷新令牌的C#sharp身份验证的Google.Apis客户端

使用刷新令牌的C#sharp身份验证的Google.Apis客户端

问题描述:

我正在使用新的Beta版.NET的Google API客户端库来加载多个用户的任务列表.它被归类为已安装的应用程序"(根据Google dev.控制台),具有多个授权用户帐户.验证一个用户(使用google.apis)确实很简单,但我不知道如何使用刷新令牌执行相同操作,以及如何使用此令牌获取服务对象.

I'm using new beta Google APIs Client Library for .NET to load tasks lists of more than one user. It's classified as "installed aplication" (according to google dev. console) with more than one authorized user accounts. It's really simple to authenticate one user (using google.apis) but I cannot figure out, how to do the same with refresh tokens, and how to use this tokens to get service object.

示例代码:

var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(GoogleTools.GenerateStreamFromString(GoogleTools.Cred)).Secrets,
new[] { TasksService.Scope.Tasks },
"user", CancellationToken.None, new FileDataStore("Tasks.Auth.Store")).Result;         


// Create the service.
service = new TasksService(new BaseClientService.Initializer()
{
     HttpClientInitializer = credential,
     ApplicationName = "Tasks API Sample",
});

我想使用刷新令牌构造对象credential,但是我真的迷路了,找不到任何合适的文档.

I want to construct object credential using refresh token, but I'm really lost and cannot find any appropriate documentation.

您可以使用以下代码检查当前令牌是否已过期并刷新:

You can check if the current Token is expired and Refresh using the code below:

if (credential.Token.IsExpired(Google.Apis.Util.SystemClock.Default))
{
  var refreshResult = credential.RefreshTokenAsync(CancellationToken.None).Result;
}

此后,凭证将刷新credential.Token.AccessTokencredential.Token.RefreshToken