gpt4 book ai didi

asp.net-mvc - OAuth 2 Google API 刷新 token 为空

转载 作者:行者123 更新时间:2023-12-02 13:26:56 26 4
gpt4 key购买 nike

我正在开发一个 Asp.NET MVC5 应用程序 following this Google sample code .

我希望用户对应用程序进行身份验证并创建访问 token (配置阶段),然后我需要能够使用刷新 token (无需用户干预,类似“离线”)。

Controller :

public async Task<ActionResult> IndexAsync(CancellationToken cancellationToken)
{
var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).
AuthorizeAsync(cancellationToken);

if (result.Credential != null)
{
var service = new Google.Apis.Admin.Directory.directory_v1.DirectoryService(new BaseClientService.Initializer
{
HttpClientInitializer = result.Credential,
ApplicationName = "My Application"
});
return View();
}
else
{
return new RedirectResult(result.RedirectUri);
}
}

FlowMetadata 实现。 (我正在使用稍微修改过的 FiledataStore (GoogleFileDataStore))

public class AppFlowMetadata : FlowMetadata
{
//Move to settings
static readonly string clientId = "xxxccnvofsdfsfoj.apps.googleusercontent.com";
static readonly string clientsecret = "xxxxxxxxxxLvtC6Qbqpp4x_";
static readonly string datastore = "AdminSDK.Api.Auth.Store";


private static readonly IAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = clientId,
ClientSecret = clientsecret
},
Scopes = new[] { DirectoryService.Scope.AdminDirectoryUser, DirectoryService.Scope.AdminDirectoryUserAliasReadonly },
DataStore = new GoogleFileDataStore(datastore)
});


public override string GetUserId(Controller controller)
{
return ConfigHelper.UserID.ToString();
}

public override IAuthorizationCodeFlow Flow
{
get { return flow; }
}
}

当我调用 IndexAsync Controller 时,由于用户之前没有访问 token ,因此它会在用户登录 Google 帐户后创建一个新的访问 token 。
这是一个示例访问 token ,

{"access_token":"ya29.5gBOszGO-oYJJt4YZfF6FeaZth1f69_.....","token_type":"承载","expires_in":3600,"已发布":"2014-12-24T16:02 :32.014+05:30"}

问题1:为什么没有存储Refreshtoken?如何检索刷新 token ?问题2:如果获得了refreshtoken,我应该如何修改代码来创建新的access token并调用API(当accesstoken过期时)?[我提到this question ,但对于缺少刷新 token 没有正确的答案。

最佳答案

找到了Google API离线访问获取刷新 token 并使用刷新 token 创建新的accesstoken的解决方案,

问题1:为什么没有存储Refreshtoken?如何检索刷新 token ?

我必须在请求中将 access_type 设置为离线(默认情况下为在线)。 as mentioned here

我必须为 GoogleAuthorizationCodeFlow 类编写自己的实现。感谢this post.

 public class ForceOfflineGoogleAuthorizationCodeFlow : GoogleAuthorizationCodeFlow
{
public ForceOfflineGoogleAuthorizationCodeFlow(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"
};
}
};

问题2:..我应该修改代码来创建新的访问 token 并调用API吗?

    //try to get results
var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).
AuthorizeAsync(cancellationToken);


//// This bit checks if the token is out of date,
//// and refreshes the access token using the refresh token.
if (result.Credential.Token.IsExpired(SystemClock.Default))
{
Google.Apis.Auth.OAuth2.Responses.TokenResponse token = new Google.Apis.Auth.OAuth2.Responses.TokenResponse();
//If the token is expired recreate the token
token = await result.Credential.Flow.RefreshTokenAsync(ConfigHelper.UserID.ToString(), result.Credential.Token.RefreshToken, CancellationToken.None);

//Get the authorization details back
result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).AuthorizeAsync(cancellationToken);
}

这对我有用!希望这会帮助其他人......

关于asp.net-mvc - OAuth 2 Google API 刷新 token 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27684334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com