Google&Oauthlib-范围已更改

Gau*_*gul 8 python python-2.7 google-oauth google-api-python-client django-1.7

我正在使用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".
Run Code Online (Sandbox Code Playgroud)

以下是用于生成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'
        )
Run Code Online (Sandbox Code Playgroud)

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

F B*_*het 8

之前的所有答案都不适合我。我通过添加这一行解决了这个问题:

os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1'
Run Code Online (Sandbox Code Playgroud)


Sym*_*ric 7

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

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

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

  • 有用!使用此标志的安全隐患是什么? (6认同)

小智 7

我能够通过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)
Run Code Online (Sandbox Code Playgroud)

  • 当我尝试这种方法时,我收到了来自 Google 的错误页面,指出必须定义“范围” (2认同)

kir*_*ran 6

甚至我也有同样的问题。我已经通过删除include_granted_scopes='true',flow.authorization_url解决了这个问题

  • 我尝试删除 `include_granted_scopes='true'` 也尝试将其设置为 `false` 但没有帮助。我遇到了同样的错误。 (2认同)