Flask 如何获得干净的URL?
在这里,当我通过谷歌登录时,我的URL更改为以下内容: 我想要的是我在URL中给出的内容Flask 如何获得干净的URL?,flask,oauth2client,Flask,Oauth2client,在这里,当我通过谷歌登录时,我的URL更改为以下内容: 我想要的是我在URL中给出的内容 我应该怎么做?这听起来像是身份验证进程回调部分的预期行为 您要做的是将用户重定向到authorized()函数末尾的主路由。该函数或多或少“属于”OAuth进程(这是一个很好的思考方式)。您只需确定该过程是否成功,然后将用户重定向到他们需要去的地方 在此过程中,我喜欢使用与用户进行沟通 例子: 您应该在这里看到会话密钥存在(例如Cyrptocokie)。。。显然,您还应该根据文档使用os.uradom(
我应该怎么做?这听起来像是身份验证进程回调部分的预期行为 您要做的是将用户重定向到
authorized()
函数末尾的主路由。该函数或多或少“属于”OAuth进程(这是一个很好的思考方式)。您只需确定该过程是否成功,然后将用户重定向到他们需要去的地方
在此过程中,我喜欢使用与用户进行沟通
例子:
您应该在这里看到会话密钥存在(例如Cyrptocokie)。。。显然,您还应该根据文档使用os.uradom(24)
设置密钥
from flask import Flask, redirect, url_for, session, request, jsonify
from flask_oauthlib.client import OAuth
app = Flask(__name__)
app.config['GOOGLE_ID'] = "12"
app.config['GOOGLE_SECRET'] = "A"BC
app.debug = True
app.secret_key = 'development'
oauth = OAuth(app)
google = oauth.remote_app(
'google',
consumer_key=app.config.get('GOOGLE_ID'),
consumer_secret=app.config.get('GOOGLE_SECRET'),
request_token_params={
'scope': 'email'
},
base_url='https://www.googleapis.com/oauth2/v1/',
request_token_url=None,
access_token_method='POST',
access_token_url='https://accounts.google.com/o/oauth2/token',
authorize_url='https://accounts.google.com/o/oauth2/auth',
)
@app.route('/')
def index():
if 'google_token' in session:
me = google.get('userinfo')
return jsonify({"data": me.data})
return redirect(url_for('login'))
@app.route('/login')
def login():
return google.authorize(callback=url_for('authorized', _external=True))
@app.route('/logout')
def logout():
session.pop('google_token', None)
return redirect(url_for('index'))
@app.route('/login/authorized')
def authorized():
resp = google.authorized_response()
if resp is None:
return 'Access denied: reason=%s error=%s' % (
request.args['error_reason'],
request.args['error_description']
)
session['google_token'] = (resp['access_token'], '')
me = google.get('userinfo')
return jsonify({"data": me.data})
@google.tokengetter
def get_google_oauth_token():
return session.get('google_token')
@app.route('/')
def index():
if 'google_token' not in session:
flash("Please log in to see this page")
return redirect(url_for('login'))
me = google.get('userinfo')
return render_template("index.html", user=me)
@app.route('/login/authorized')
def authorized():
resp = google.authorized_response()
if resp is None:
flash("Access denied: reason={0} error={1}".format(
request.args['error_reason'],
request.args['error_description']
))
return redirect(url_for("login"))
session['google_token'] = (resp['access_token'], '')
flash("Successful login!") # superfluous, just for example
return redirect(url_for("index"))