在上周二之前,我们的应用程序一直运行良好。在为 google plus 交换 oauth2 token 时,我们不断收到“policy_enforced”错误。谷歌的回应是:
Google.Apis.Auth.OAuth2.Responses.TokenResponseException:
Error:"policy_enforced",
Description:"Access denied by a security policy established by the Google Apps administrator of your organization. Please contact your administrator for further assistance.",
Uri:""
我们使用的是 google-api-dotnet-client,代码非常简单。
var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
DataStore = new CustomGoogleDataStore(),
ClientSecrets = new global::Google.Apis.Auth.OAuth2.ClientSecrets { ClientId = client.ID, ClientSecret = client.Secret },
Scopes = scopes.Split(' '),
});
var token = flow.ExchangeCodeForTokenAsync(guid, code, redirectUri, CancellationToken.None).Result;
范围是:
"https://www.googleapis.com/auth/plus.login",
"https://www.googleapis.com/auth/plus.profile.emails.read",
"https://www.googleapis.com/auth/plus.me",
"https://www.googleapis.com/auth/userinfo.profile",
"https://www.googleapis.com/auth/userinfo.email",
"https://mail.google.com/"
这个问题是随机发生的。直到现在我都找不到 100% 重现它的方法。
请让我知道这是什么问题,如有任何建议,我们将不胜感激。
最佳答案
我最近遇到了同样的问题。联系 Google 团队后,我们发现此问题是由于授权范围与我们在登录时请求的范围不匹配而导致的。我们使用了最新的授权范围 profile和 email用户登录时。但在 GAM API 控制台中,Google 对已弃用的范围进行了硬编码
https://www.googleapis.com/auth/userinfo.profile
https://www.googleapis.com/auth/userinfo.email
将最新的授权范围恢复为弃用的范围后,登录工作正常。
关于google-api - 为 google plus 交换 oauth2 token 时出现 "policy_enforced"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25508740/