谷歌和Oauthlib - 范围已更改

20

我正在使用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" 的范围完全相同。这可能是谷歌端的一个错误吗? - ack_inc
7个回答

22

12
可以!使用这个标志会有哪些安全影响? - odedfos
1
授权范围可能与请求的不同。如果授予的权限比请求的少,功能上可能会出现问题。从安全角度来看,您不一定希望授予更多的权限,但这正是Google库中include_granted_scopes=True所做的:https://developers.google.com/identity/protocols/oauth2/web-server#incrementalAuth - Neil C. Obremski
如果您控制调用oauth库的代码,那么正确的方式是什么? - Alex
使用FastAPI,我注意到捕获Warning对象并打印它只会打印出Scope has changed from...等等,而使用jsonable_encoder会返回整个凭证对象,包括旧和新的范围。所以最后我捕获了Warning,将JSON编码的警告作为响应返回。我猜这就是他们想要的工作方式吧? - lupodellasleppa

11
我能通过在回调函数中将作用域设置为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)

2
当我尝试这种方法时,从谷歌那里收到了一个错误页面,说必须定义“scopes”。 - Neil C. Obremski

6

之前的所有回答对我都没有用。我通过添加这一行代码解决了问题:

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

6

我也遇到了同样的问题。我通过在flow.authorization_url中删除include_granted_scopes='true'来解决了这个问题。


2
我尝试删除 include_granted_scopes='true',也尝试将其设置为 false,但没有帮助。我仍然得到相同的错误。 - Gaurav Bagul

3

我不确定这是否是错误,但范围可能应该是列表范围,而不是一个字符串 - 将其更改为:

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
        )

2

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

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,我仍然遇到相同的错误。 - Gaurav Bagul

0

因为你设置了 include_granted_scopes='true',所以当你再次运行时作用域将被追加。

让我们设置 include_granted_scopes='false'


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接