【发布时间】:2021-08-20 09:26:29
【问题描述】:
我正在使用 Google 的 OAuth .Net 库在 ASP.Net MVC 应用程序中实现 Google OAuth。下面是我的代码。
IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer {
ClientSecrets = new ClientSecrets {
** ClientId ** , ** ClientSecret **
},
DataStore = new FileDataStore( ** responsepath ** , true),
Scopes = new [] {
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/gmail.send"
},
Prompt = "select_account"
});
var userId = "user";
var uri = Request.Url.ToString();
var code = Request["code"];
if (code != null) {
var token = flow.ExchangeCodeForTokenAsync(userId, code, uri.Substring(0, uri.IndexOf("?")), CancellationToken.None).Result;
var oauthState = AuthWebUtility.ExtracRedirectFromState(flow.DataStore, userId, Request["state"]).Result;
Response.Redirect(oauthState);
} else {
var result = new AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(userId, CancellationToken.None).Result;
if (result.RedirectUri != null) {
Response.Redirect(result.RedirectUri);
}
}
当用户点击谷歌登录按钮时,我的页面被重定向到谷歌认证页面。认证成功后,再次显示我的页面。当我检查 responsepath 时,会创建以下文件,其中包含访问令牌、到期时间等。
Google.Apis.Auth.OAuth2.Responses.TokenResponse-用户
当我在我的 Visual Studio 调试环境(IIS Express)中本地运行上述代码时,上述响应文件中包含“refresh_token”。当在生产环境(IIS)中部署相同的代码时,缺少“refresh_token”是响应文件。我想知道它背后的原因,因为我需要刷新令牌进行进一步处理。
注意:在这两种情况下,我在尝试之前都从我的 Google 帐户撤消了应用程序的访问权限。所以,这不是关于“refresh_token”只会第一次发送。
【问题讨论】:
标签: c# oauth-2.0 google-oauth google-api-dotnet-client