Max*_*Max 7 oauth flask google-oauth flask-oauthlib
我有一个用Flask编写的应用程序,并尝试使用Flask-Dance(Flask-Dance Docs - Google示例)来启用Google OAuth.我得到了以下设置:
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)
Run Code Online (Sandbox Code Playgroud)
然后在app/__init__.py:
from server.app.auth import auth, google_login
app.register_blueprint(auth)
app.register_blueprint(google_login, url_prefix='/google_login')
Run Code Online (Sandbox Code Playgroud)
通过点击应用程序中的按钮我去/auth/google那里(重定向后)我可以看到一个谷歌帐户列表可供选择.当我在Network dev工具中选择一个帐户时,我看到以下路由(缺少url参数):
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=
...一切都从头开始,我看到一个"选择帐户"屏幕.
在Google API帐户中,我有一个重定向网址:
http://127.0.0.1:8001/google_login/google/authorized
在开发环境中我设置OAUTHLIB_INSECURE_TRANSPORT=1与OAUTHLIB_RELAX_TOKEN_SCOPE=1
看起来路由中的第三个URL应该是/auth/google并尝试google.authorized再次解析,但它没有,我print(google.authorized) # False只看到一次点击应用程序内的谷歌按钮的结果.
make_google_blueprint默认情况下,生成的蓝图将重定向到/认证周期结束时;您可以使用或参数进行配置。在您的情况下:redirect_urlredirect_to
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'
)
Run Code Online (Sandbox Code Playgroud)
编辑:还请确保您的应用程序设置良好 secret_key。
| 归档时间: |
|
| 查看次数: |
1454 次 |
| 最近记录: |