Google API范围已更改

Jul*_*ing 8 django google-api python-3.x google-oauth

编辑:我通过在我的示波器中添加" 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"])
Run Code Online (Sandbox Code Playgroud)

返回网址是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"])
    ...
Run Code Online (Sandbox Code Playgroud)

小智 6

我们今天发现了同样的问题。在过去的几个月里,我们的解决方案一直运行良好,没有出现任何问题。

我们通过将原始范围“个人资料电子邮件”更新为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)
Run Code Online (Sandbox Code Playgroud)

现在,通过更新的范围,我们发送一个列表,其中每个元素都是一个单独的范围。

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)
Run Code Online (Sandbox Code Playgroud)

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