Flask-Dance Error: Scope has changed

Flask-Dance Error: Scope has changed

我正在使用 flask-dance 对 Google 的服务器进行身份验证。

烧瓶舞配置:

from flask.ext.dance.contrib.google import make_google_blueprint
google_blueprint = make_google_blueprint (
    client_id=app.config['GOOGLE']['client_id'],
    client_secret=app.config['GOOGLE']['client_secret'],
    scope=["profile", "email"],
    redirect_to="main.index",
    login_url="/",
    authorized_url="/authorized",
)
app.register_blueprint(google_blueprint,url_prefix="/login")

但是,在您完成 Google 的凭据对话框后,我得到 Warning: Scope has changed from "profile email" to "".

这是完整的跟踪:

Traceback (most recent call last):
  File "/home/xxx/.virtualenvs/flask/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/xxx/.virtualenvs/flask/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/xxx/.virtualenvs/flask/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/xxx/.virtualenvs/flask/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/xxx/.virtualenvs/flask/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/xxx/.virtualenvs/flask/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/xxx/.virtualenvs/flask/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/xxx/.virtualenvs/flask/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/xxx/.virtualenvs/flask/lib/python2.7/site-packages/flask_dance/consumer/oauth2.py", line 168, in authorized
    client_secret=self.client_secret,
  File "/home/xxx/.virtualenvs/flask/lib/python2.7/site-packages/requests_oauthlib/oauth2_session.py", line 199, in fetch_token
    self._client.parse_request_body_response(r.text, scope=self.scope)
  File "/home/xxx/.virtualenvs/flask/lib/python2.7/site-packages/oauthlib/oauth2/rfc6749/clients/base.py", line 409, in parse_request_body_response
    self.token = parse_token_response(body, scope=scope)
  File "/home/xxx/.virtualenvs/flask/lib/python2.7/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 376, in parse_token_response
    validate_token_parameters(params)
  File "/home/xxx/.virtualenvs/flask/lib/python2.7/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 406, in validate_token_parameters
    raise w
Warning: Scope has changed from "profile email" to "".

在 Chrome 开发控制台上,我得到了这个(在帐户选择器 window 之后,就在回溯之前):

GET http://localhost:5000/login/authorized?state=11rtXcAHJm0jloiDpM8IrazD9uLT3b…xnXM0ZB1DumOPqFOgu-x19CDSbDfQoKLWVEfBRTQIg.gvDk1rm330AV3oEBd8DOtNAR0Vr7lQI 500 (INTERNAL SERVER ERROR)
Navigated to http://localhost:5000/login/authorized?state=11rtXcAHJm0jloiDpM8IrazD9uLT3b…xnXM0ZB1DumOPqFOgu-x19CDSbDfQoKLWVEfBRTQIg.gvDk1rm330AV3oEBd8DOtNAR0Vr7lQI

如果我这样做 os.environ['OAUTHLIB_RELAX_TOKEN_SCOPE'] = '1' 它会起作用,但我认为这不是解决方案,而是目前更多的解决方法。 :(

我就这个问题开了工单Google OAuth2 returns no scope on authentication breaks scope test #306关于这个问题

据我所知 https://www.rfc-editor.org/rfc/rfc6749#section-3.3,return 范围不需要 Google(除非范围已更改),我读得正确吗?

我能想到的最接近的事情是 oauthlib plugin which I opened a bug report 中已经有一个错误。

根据错误报告,您可以通过将第 30 行插件中的文件 /oauth2/rfc6749/tokens.pyself._new_scope = set(utils.scope_to_list(params.get('scope', ''))) 更改为 self._new_scope = set(utils.scope_to_list(params.get('scope', old_scope)))

来解决问题

修复已合并:https://github.com/idan/oauthlib/pull/323