C# 如何在C语言的Google.api.core1.9中将AccessType设置为脱机#
我正在为OAuth2版本1.9使用GoogleAPI,每次都尝试将AccessType作为offline发送,将ApprovalPrompt作为force发送,以便获得一个刷新令牌。我知道在不同的api版本和语言中有很多关于这个主题的问题。然而,这些解决方案都不能与新的google库配合使用 我使用以下方法获得流:C# 如何在C语言的Google.api.core1.9中将AccessType设置为脱机#,c#,oauth-2.0,google-api,google-api-dotnet-client,C#,Oauth 2.0,Google Api,Google Api Dotnet Client,我正在为OAuth2版本1.9使用GoogleAPI,每次都尝试将AccessType作为offline发送,将ApprovalPrompt作为force发送,以便获得一个刷新令牌。我知道在不同的api版本和语言中有很多关于这个主题的问题。然而,这些解决方案都不能与新的google库配合使用 我使用以下方法获得流: private IAuthorizationCodeFlow GetAuthorizationCodeFlow() { var flow =
private IAuthorizationCodeFlow GetAuthorizationCodeFlow()
{
var flow = new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId =
"***",
ClientSecret = "***"
},
Scopes = new string[]
{
CalendarService.Scope.Calendar,
PlusService.Scope.UserinfoProfile,
PlusService.Scope.UserinfoEmail,
PlusService.Scope.PlusLogin,
"https://www.googleapis.com/auth/contacts.readonly"
}
});
return flow;
}
然后使用以下代码获取令牌:
var token = flow.ExchangeCodeForTokenAsync("me", code,
uri.Substring(0, uri.IndexOf("?")), CancellationToken.None).Result;
这就是我每次(不仅仅是第一次)都需要刷新令牌的地方,所以我想设置AccessType和ApprovalPrompt。我也有同样的问题。功劳归于我。 我将粘贴我的代码,因为我必须做一个小的更改才能让它工作。 (在构造函数中使用GoogleAuthorizationCodeFlow.Initializer,而不是AuthorizationCodeFlow.Initializer) 实现您自己的GoogleAuthorizationCodeFlow类,并在AccessType属性中设置“脱机”访问。这将为您提供刷新令牌
public class OfflineAccessGoogleAuthorizationCodeFlow : GoogleAuthorizationCodeFlow
{
public OfflineAccessGoogleAuthorizationCodeFlow(GoogleAuthorizationCodeFlow.Initializer initializer) : base(initializer) { }
public override AuthorizationCodeRequestUrl CreateAuthorizationCodeRequest(string redirectUri)
{
return new GoogleAuthorizationCodeRequestUrl(new Uri(AuthorizationServerUrl))
{
ClientId = ClientSecrets.ClientId,
Scope = string.Join(" ", Scopes),
RedirectUri = redirectUri,
AccessType = "offline",
ApprovalPrompt = "force"
};
}
};
不幸的是,我们无法从GoogleAuthorizationCodeFlow初始值设定项设置“访问类型”
另外,我使用以下代码检索刷新令牌
Google.Apis.Auth.OAuth2.Responses.TokenResponse token = new Google.Apis.Auth.OAuth2.Responses.TokenResponse();
//// Checks if the token is out of date and refresh the access token using the refresh token.
if (result.Credential.Token.IsExpired(SystemClock.Default))
{
//If the token is expired recreate the token
token = await result.Credential.Flow.RefreshTokenAsync(userid.ToString(), result.Credential.Token.RefreshToken, CancellationToken.None);
//Get the authorization details Results
result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).AuthorizeAsync(cancellationToken);
}
你到底想干什么?库自动为用户存储刷新令牌,以便在其访问令牌过期时刷新。通过将“访问类型”设置为“脱机”,您到底想实现什么?我们不使用库存储机制。我们正在使用旧库并升级到v3。我们将刷新令牌存储在数据库字段中,并使用它每次获取访问令牌。因此,当刷新令牌不存在或调用失败时,我们希望再次验证并获取刷新令牌。希望这是有意义的。如果您希望您的用户再次验证以获取刷新令牌(以及访问令牌),则无需执行任何操作。只需升级您的库,用户就必须首先进行身份验证。如果要使用旧的刷新令牌升级库,则情况不同。您希望支持这两种情况中的哪一种?我们可以通过在数据库中使刷新令牌为空来强制用户再次进行身份验证。这不是问题所在。我正在寻找的灵活性,以获得刷新令牌任何时候我们想要的。默认行为是,一旦发送了刷新令牌,在刷新令牌的生命周期内将不再发送该令牌,并且将发送null。我看到协议允许我通过操纵AccessType和ApprovalPrompt随时获取刷新令牌。但是,我不知道什么api调用允许我这样做。我不知道,你能给我看一下你正在使用的文档吗?