我正在使用适用于 .NET 的新测试版 Google API 客户端库来加载多个用户的任务列表。它被归类为具有多个授权用户帐户的“已安装应用程序”(根据 google dev.console)。对一个用户进行身份验证(使用 google.apis)真的很简单,但我不知道如何使用刷新 token 执行相同的操作,以及如何使用此 token 获取服务对象。
示例代码:
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",
});
我想使用刷新 token 构造对象 credential
,但我真的迷路了,找不到任何合适的文档。
最佳答案
您可以使用以下代码检查当前 token 是否过期并刷新:
if (credential.Token.IsExpired(Google.Apis.Util.SystemClock.Default))
{
var refreshResult = credential.RefreshTokenAsync(CancellationToken.None).Result;
}
之后,凭据刷新了 credential.Token.AccessToken
和 credential.Token.RefreshToken
关于c# - 使用刷新 token 的 C#sharp 身份验证的 Google.Apis 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22357348/