编辑:我通过在我的范围中添加“https://www.googleapis.com/auth/plus.me”轻松解决了这个问题,但我想开始讨论这个主题,看看是否有其他人经历过同样的事情问题。
我有一个在 GCP 上运行的服务,这是一个使用 Google API 的应用程序引擎。今天早上,我收到了这条“警告”消息,引发了 500 错误。 过去一个月一直运行良好,今天才抛出此错误(在本文发布前 5 小时)。
有谁知道为什么 Google 在 oauth2callback 中返回额外的范围? 非常感谢任何额外的见解。如果您以前见过此内容,请告诉我。我到处都找不到。
异常类型:/oauth2callback 处警告
异常值: 范围已更改为 “https://www.googleapis.com/auth/userinfo.email”至 “https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.me”。
这行抛出了错误:
flow.fetch_token(
authorization_response=authorization_response,
code=request.session["code"])
编辑:示例代码
import the required things
SCOPES = ['https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/calendar',
# 'https://www.googleapis.com/auth/plus.me' <-- without this, it throws the error stated above. adding it, fixes the problem. Google returns an additional scope (.../plus.me) which causes an error.
]
def auth(request):
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
CLIENT_SECRETS_FILE, scopes=SCOPES)
flow.redirect_uri = website_url + '/oauth2callback'
authorization_url, state = flow.authorization_url(
access_type='offline', include_granted_scopes='true',
prompt='consent')
request.session["state"] = state
return redirect(authorization_url)
def oauth2callback(request):
...
# request.session["code"] = code in url
authorization_response = website_url + '/oauth2callback' + parsed.query
flow.fetch_token(
authorization_response=authorization_response,
code=request.session["code"])
...
我们今天发现了同样的问题。在过去的几个月里,我们的解决方案一直运行顺利,没有出现任何问题。
我们通过将原始范围 “个人资料电子邮件” 更新为 https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo 解决了该问题。 profile 并对代码进行一些小的更改。
启动 google_auth_oauthlib.flow 客户端时,我们之前在列表中传入了范围,其中只有一项包含一个字符串,其中范围由空格分隔。
google_scopes = 'email profile'
self.flow = Flow.from_client_secrets_file(secret_file, scopes=[google_scopes], state=state)
现在,通过更新的范围,我们发送一个列表,其中每个元素都是一个单独的范围。
google_scopes = 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'
self.flow = Flow.from_client_secrets_file(secret_file, scopes=google_scopes.split(' '), state=state)
希望对您有帮助,祝您好运!
对于我的情况,我在该函数中添加了以下行,身份验证发生在
os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'
flow = InstalledAppFlow.from_client_config(client_config, scopes=SCOPES)
我正在使用 requests_oauthlib 扩展,并且遇到了同样的错误。我通过将
OAUTHLIB_RELAX_TOKEN_SCOPE: '1'
添加到环境变量来解决此问题。所以我的 app.yaml
文件与此类似:
#...
env_variables:
OAUTHLIB_RELAX_TOKEN_SCOPE: '1'
鉴于时间安排,您可能会受到 Google 此项更改的影响: “从 2017 年 7 月 18 日开始,请求某些敏感 OAuth 范围的 Google OAuth 客户端将接受 Google 的审核。” https://developers.google.com/apps-script/guides/client-verification
根据您的错误猜测,您似乎正在使用折旧的范围。看: https://developers.google.com/+/web/api/rest/oauth#deprecated-scopes
我还猜测您可能正在使用 Google+ Platform Web 库,也可能使用 People:Get 方法。也许尝试使用以下范围之一:
https://www.googleapis.com/auth/plus.login
或
https://www.googleapis.com/auth/plus.me
按如下方式更新我的范围并且它有效
SCOPES = [
"openid",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
]
添加了 openid 其他范围