【问题标题】:Google API Scope ChangedGoogle API 范围已更改
【发布时间】:2018-12-27 02:55:01
【问题描述】:

编辑:我通过将“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"])

返回地址为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"])
    ...

【问题讨论】:

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


【解决方案1】:

对于我的情况,我在该函数中添加了以下行来进行身份验证 os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1' flow = InstalledAppFlow.from_client_config(client_config, scopes=SCOPES)

【讨论】:

    【解决方案2】:

    我正在使用 requests_oauthlib 扩展,但我遇到了同样的错误。我通过将OAUTHLIB_RELAX_TOKEN_SCOPE: '1' 添加到环境变量来解决此问题。所以我的app.yaml 文件是这样的:

    #...
    env_variables:
      OAUTHLIB_RELAX_TOKEN_SCOPE: '1'
    

    【讨论】:

    • 来自flask_dance的问题的解决方案
    【解决方案3】:

    鉴于时间安排,您可能会受到 Google 此次更改的影响: “从 2017 年 7 月 18 日开始,请求某些敏感 OAuth 范围的 Google OAuth 客户端将接受 Google 的审查。” https://developers.google.com/apps-script/guides/client-verification

    【讨论】:

      【解决方案4】:

      我们今天发现了同样的问题。在过去的几个月里,我们的解决方案一直在正常工作。

      我们通过将原始范围 'profile email' 更新为 https://www.googleapis.com/auth/userinfo.emailhttps://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)
      

      希望对你有帮助,祝你好运!

      【讨论】:

      • 谢谢。我已经通过将它添加到范围来解决它。我应该更清楚我的目标是什么,抱歉。你知道为什么会变吗?
      • @JuliusTing 我很高兴它成功了! :) 不知道为什么范围发生了变化,在 Google 上四处寻找,但没有真正找到任何东西。
      • @JuliusTing 如果答案有帮助,您介意将其标记为解决方案吗?
      【解决方案5】:

      根据您的错误猜测,您似乎使用的是折旧范围。看: https://developers.google.com/+/web/api/rest/oauth#deprecated-scopes

      我还猜测您可能正在使用Google+ Platform Web libraryPeople:Get 方法。也许尝试使用以下范围之一:

      https://www.googleapis.com/auth/plus.login
      

      https://www.googleapis.com/auth/plus.me
      

      【讨论】:

      • 嗨。感谢您的评论。是的,这是一个已弃用的范围,谷歌提到“它将被维护并保持可用以实现向后兼容性”。所以我认为它会经历最少的变化
      猜你喜欢
      • 2019-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 2016-06-27
      • 1970-01-01
      • 2014-11-15
      • 2014-05-16
      • 1970-01-01
      相关资源
      最近更新 更多