C# 谷歌驱动器服务帐户刷新令牌
我的会话每小时过期一次,在使用服务帐户身份验证方法时,我找不到有关如何刷新令牌的文档。对于已安装的应用程序,我可以从C# 谷歌驱动器服务帐户刷新令牌,c#,google-drive-api,google-oauth,google-api-client,C#,Google Drive Api,Google Oauth,Google Api Client,我的会话每小时过期一次,在使用服务帐户身份验证方法时,我找不到有关如何刷新令牌的文档。对于已安装的应用程序,我可以从state对象获取refreshtToken AuthorizationState state = new AuthorizationState(new[] { "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/drive.file", "https://www
state
对象获取refreshtToken
AuthorizationState state = new AuthorizationState(new[]
{
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive.metadata.readonly",
"https://www.googleapis.com/auth/drive.readonly"
})
{
Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl)
}
state = client.ProcessUserAuthorization(GetAuthorizationCode(), state);
Console.WriteLine(state.RefreshToken);
但如何为服务帐户做到这一点
X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "mysecret", X509KeyStorageFlags.Exportable);
var provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, certificate)
{
ServiceAccountId = SERVICE_ACCOUNT_EMAIL,
Scope = DriveService.Scopes.Drive.GetStringValue(),
ServiceAccountUser = "myemail@mydomain.com",
};
var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);
DriveService service = DriveService(auth);
看起来它确实刷新了令牌。我将这个函数调用添加到我的令牌刷新计时器中,但它没有帮助。一小时后,我仍会继续收到
无效凭据
异常。服务帐户不使用刷新令牌。您只需使用第一次使用的相同过程请求另一个访问令牌。请参阅谷歌关于如何做的文档。您能再澄清一下您的要求吗?我从上面了解到的是,您有刷新令牌,您只需要新的访问令牌,对吗?谢谢。但我该怎么做呢?我正在使用Google客户端SDK,是否需要删除旧对象并为此创建一个新的DriveService
对象?
IAuthorizationState state = new AuthorizationState(provider.Scope.Split(' '));
if (provider.RefreshToken(state, null)) {
return state;
} else {
return null;
}