django - Google API 范围已更改

标签 django python-3.x google-api google-oauth

编辑:我通过在范围中添加“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"])

返回网址为https://my_website.com/oauth2callback?state=SECRET_STATE&scope=https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/plus.me#

而不是通常的https://my_website.com/oauth2callback?state=SECRET_STATE&scope=https://www.googleapis.com/auth/userinfo.email#

编辑:示例代码

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/

相关文章:

python - 如何分析 python 进程的内存使用情况?

python - 如何将数据库中 Django 模型的 "pickle"实例转换为可用于加载示例数据的示例 python 代码?

python - Python 中的 REST API 与 FastAPI 和 pydantic : read-only property in model

OAuth 2.0 与 Google Analytics API v3

python - 无法从ManyToManyField获取相关数据

django - 在 Postgres 中锁定表以允许其他事务读取,但不允许写入

python - 类型错误 : open() takes 0 positional arguments but 2 were given

python - Python 3 中的 super() 怪异之处

android - 无法使用 Google 的融合位置 API 获取位置

php - 是否可以从我客户的网站获取数据?