使用刷新令牌的C#sharp身份验证的Google.Apis客户端 [英] Google.Apis client for C#sharp auth using refresh token

查看:128
本文介绍了使用刷新令牌的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

这篇关于使用刷新令牌的C#sharp身份验证的Google.Apis客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆