编辑:我通过在范围中添加“https://www.googleapis.com/auth/plus.me ”轻松解决了这个问题,但我想开始讨论这个主题,看看是否有其他人遇到同样的问题。
我有一个在 GCP 上运行的服务,这是一个使用 Google API 的应用引擎。今天早上,我收到了这条“警告”消息,引发了 500 错误。 过去一个月一直运行良好,今天才抛出此错误(在本文发布前 5 小时)。
有人知道为什么 Google 在 oauth2callback 中返回额外的范围吗?非常感谢任何额外的见解。如果您以前见过此内容,请告诉我。我到处都找不到它。
Exception Type: Warning at /oauth2callback
Exception Value: Scope has changed from "https://www.googleapis.com/auth/userinfo.email" to "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)
希望对你有帮助,祝你好运!
关于django - Google API 范围已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51414685/