【问题标题】:Google.Apis client for C#sharp auth using refresh token使用刷新令牌的 C#sharp 身份验证的 Google.Apis 客户端
【发布时间】:2014-04-16 22:21:23
【问题描述】:

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

示例代码:

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,但我真的迷路了,找不到任何合适的文档。

【问题讨论】:

标签: c# oauth-2.0 google-api google-api-dotnet-client


【解决方案1】:

我已经在下面的帖子中回答了这个问题: .NET Google api 1.7 beta authenticating with refresh token

希望对你也有帮助。

【讨论】:

    【解决方案2】:

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

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

    之后,凭据刷新了 credential.Token.AccessTokencredential.Token.RefreshToken

    【讨论】:

    • 这会更新 FileDataStore 对象吗?与其他答案相比,您的建议似乎很简单。
    • GoogleCredential.FromAccessToken 没有RefreshTokenAsync
    猜你喜欢
    • 2021-12-16
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    • 1970-01-01
    • 2020-07-07
    • 2016-04-04
    相关资源
    最近更新 更多