问题描述
我有一个使用Flask编写的应用,并尝试使用Flask-Dance( Flask-Dance文档-Google示例)以启用Google OAuth.我有以下设置:
I have an app written with Flask and try to use Flask-Dance (Flask-Dance Docs - Google Example) to enable Google OAuth. I got the following setup:
from flask import redirect, url_for, jsonify, Blueprint
from flask_dance.contrib.google import make_google_blueprint, google
from server.app import app
# Internal auth blueprint
auth = Blueprint('auth', __name__, url_prefix='/auth')
# Google auth blueprint
google_login = make_google_blueprint(
client_id=app.config['GOOGLE_CLIENT_ID'],
client_secret=app.config['GOOGLE_CLIENT_SECRET'],
scope=['profile', 'email']
)
def auth_google_view():
"""
Authenticate user with google
"""
# Not authorized
print(google.authorized)
if not google.authorized:
return redirect(url_for('google.login'))
# Authorized - check data
user_info = google.get('/oauth2/v2/userinfo')
if user_info.ok:
return jsonify({'status': 'ok', 'email': user_info.json() .['email']}), 200
return jsonify({'status': 'failed'})
# Add urls
auth.add_url_rule('/google', view_func=auth_google_view)
然后在app/__init__.py
中:
from server.app.auth import auth, google_login
app.register_blueprint(auth)
app.register_blueprint(google_login, url_prefix='/google_login')
通过在应用程序中单击按钮,我转到/auth/google
,在那里(重定向后),我可以看到一个可供选择的google帐户列表.当我在Network开发工具中选择一个帐户时,会看到以下路由(缺少URL参数):
By clicking on button in the app I go to /auth/google
and there (after redirects) I can see a google accounts list to choose from. When I select an account in the Network dev tools I see the following routing (url parameters missing):
-
https://accounts.google.com/_/signin/oauth?authuser=
-
http://127.0.0.1:8001/google_login/google/authorized?state=
-
http://127.0.0.1:8001/google_login/google
https://accounts.google.com/_/signin/oauth?authuser=
http://127.0.0.1:8001/google_login/google/authorized?state=
http://127.0.0.1:8001/google_login/google
然后:
-
https://accounts.google.com/o/oauth2/auth?response_type=
...
https://accounts.google.com/o/oauth2/auth?response_type=
...
一切都从头开始,我看到一个选择帐户"屏幕.
all starts from the beginning and I see a "choose account" screen.
在Google API帐户中,我有一个重定向网址:
In the Google API account I have a redirect url:
http://127.0.0.1:8001/google_login/google/authorized
在开发环境中,我设置了OAUTHLIB_INSECURE_TRANSPORT=1
和OAUTHLIB_RELAX_TOKEN_SCOPE=1
In the development environment I set OAUTHLIB_INSECURE_TRANSPORT=1
and OAUTHLIB_RELAX_TOKEN_SCOPE=1
似乎路由中的第三个URL应该是/auth/google
并尝试再次解析google.authorized
,但是没有,当我单击应用程序内的google按钮时,我只能看到一次print(google.authorized) # False
的结果./p>
It seems like the third URL in routing should be /auth/google
and try to resolve google.authorized
once again but it does not and I see result of print(google.authorized) # False
only once when click on a google button inside the app.
推荐答案
当身份验证周期结束时,make_google_blueprint
生成的蓝图默认为重定向到/
.您可以使用配置此参数redirect_url
或redirect_to
.就您而言:
The blueprint generated by make_google_blueprint
defaults to redirecting towards /
when the authentication cycle has ended; you can configure this using the parameters redirect_url
or redirect_to
. In your case:
google_login = make_google_blueprint(
client_id=app.config['GOOGLE_CLIENT_ID'],
client_secret=app.config['GOOGLE_CLIENT_SECRET'],
scope=['profile', 'email'],
redirect_to='auth.auth_google_view'
)
还要确保您的应用设置了好 secret_key
设置.
Also make sure your app has a good secret_key
set.
这篇关于带有Flask-Dance的Google OAuth(始终重定向到“选择帐户" google页面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!