【问题标题】:Google & Oauthlib - Scope has changedGoogle 和 Oauthlib - 范围已更改
【发布时间】:2019-01-01 02:24:41
【问题描述】:

我正在使用 OAuthlib 来执行 Google 的 OAuth 流程。它工作了 4 到 5 个月。突然我开始出现以下错误:

File "/home/whitesnow-2/Gaurav/Axonator/AxVirtualEnv/local/lib/python2.7/site-packages/oauthlib/oauth2/rfc6749/parameters.py", 
line 409, in validate_token_parameters raise w Warning: Scope has changed from 
"https://www.googleapis.com/auth/calendar 
https://www.googleapis.com/auth/docs 
https://www.googleapis.com/auth/spreadsheets 
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/userinfo.email 
https://www.googleapis.com/auth/userinfo.profile" to 
"https://www.googleapis.com/auth/calendar 
https://www.googleapis.com/auth/docs 
https://www.googleapis.com/auth/spreadsheets 
https://www.googleapis.com/auth/drive.file 
https://www.googleapis.com/auth/userinfo.email 
https://www.googleapis.com/auth/userinfo.profile".

以下是生成 OAuth 授权 URL 的代码:

flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
    settings.GOOGLE_OAUTH2_CLIENT_SECRETS_JSON,
    scopes=['https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/docs https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'],
    redirect_uri=REDIRECT_URI
)
authorization_url, state = flow.authorization_url(
    access_type='offline',
    include_granted_scopes='true',
    prompt='consent'
)

以下是 Google OAuth 回调的代码:

auth_code = request.GET.get("code")
objectid = request.GET.get("state")
error = request.GET.get("error")
if error == "access_denied":
    return "Access Denied"
else:
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        settings.GOOGLE_OAUTH2_CLIENT_SECRETS_JSON,
        scopes=['https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/docs https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'],
        redirect_uri=REDIRECT_URI
    )
    flow.fetch_token(code=auth_code)

【问题讨论】:

  • “from”和“to”作用域完全相同,这真是太奇怪了。这可能是 Google 的错误导致的结果吗?

标签: python python-2.7 google-oauth google-api-python-client django-1.7


【解决方案1】:

您可以通过设置OAUTHLIB_RELAX_TOKEN_SCOPE 环境变量来禁用此警告;这应该适用于您不控制调用 oauth 库的代码的情况。

这是在 oauthlib 库中实现的地方:

https://github.com/oauthlib/oauthlib/blob/master/oauthlib/oauth2/rfc6749/parameters.py#L401

【讨论】:

  • 有效!使用这个标志会有什么安全隐患?
  • 授权范围可能与请求的范围不同。如果授予的范围 比请求的范围少,则功能上可能会出现问题。安全方面,您不一定希望授予 更多 范围,但这正是 include_granted_scopes=True 在 Google 库中所做的:developers.google.com/identity/protocols/oauth2/…
【解决方案2】:

通过在回调函数中将作用域设置为None,我能够绕过该问题。

flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        settings.GOOGLE_OAUTH2_CLIENT_SECRETS_JSON,
        scopes=None,
        redirect_uri=REDIRECT_URI
    )
flow.fetch_token(code=auth_code)

【讨论】:

  • 当我尝试这种方法时,我从 Google 收到了一个错误页面,说必须定义 scopes
【解决方案3】:

即使我也有同样的问题。我已通过删除 flow.authorization_url 中的include_granted_scopes='true', 来解决此问题

【讨论】:

  • 我尝试删除 include_granted_scopes='true' 也尝试将其设置为 false 但没有帮助。我遇到了同样的错误。
【解决方案4】:

之前的所有答案都对我不起作用。我通过添加这一行解决了它:

os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'

【讨论】:

    【解决方案5】:

    我将https://www.googleapis.com/auth/plus.me 范围添加到我创建Flow 对象的位置:

    Flow.from_client_config(
        secrets_json_string,
        scopes=[
            (…),
            'https://www.googleapis.com/auth/plus.me',
        ],
        redirect_uri=redirect_url
    )
    

    【讨论】:

    • 不幸的是,即使在流范围中包含https://www.googleapis.com/auth/plus.me,我也会遇到同样的错误。
    【解决方案6】:

    我不知道这是错误,但范围应该是列表范围而不是一个字符串 - 更改此:

    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
                settings.GOOGLE_OAUTH2_CLIENT_SECRETS_JSON,
                scopes=['https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/docs https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'],
                redirect_uri=REDIRECT_URI
            )
    

    到这里:

    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
                settings.GOOGLE_OAUTH2_CLIENT_SECRETS_JSON,
                scopes=[
                    'https://www.googleapis.com/auth/calendar', 
                    'https://www.googleapis.com/auth/docs', 
                    'https://www.googleapis.com/auth/spreadsheets', 
                    'https://www.googleapis.com/auth/drive.file', 
                    'https://www.googleapis.com/auth/userinfo.email', 
                    'https://www.googleapis.com/auth/userinfo.profile'],
                redirect_uri=REDIRECT_URI
            )
    

    【讨论】:

      猜你喜欢
      • 2021-09-20
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多