我在使用Flask会话时遇到了麻烦。它在重定向后丢失了值,这里的代码我使用:
@app.route('/')
def index():
session['permanent'] = True
return flask.render_template('index.html') ##Here is template with form that passes value via GET to /station
@app.route('/station', methods=['GET'])
def station():
if 'st' not in session:
p = flask.request.args.get('Station', '')
session['st'] = p
print session['st']
if 'credentials' not in session:
return flask.redirect(flask.url_for('oauth2callback'))
credentials = client.OAuth2Credentials.from_json(session['credentials'])
if credentials.access_token_expired:
return flask.redirect(flask.url_for('oauth2callback'))
else:
##DO SOME STUFF WITH st and credentials##
return "Done!"
@app.route('/oauth2callback')
def oauth2callback():
flow = client.flow_from_clientsecrets('client_secret.json',scope='https://www.googleapis.com/auth/youtube',redirect_uri=flask.url_for('oauth2callback', _external=True))
if 'code' not in flask.request.args:
auth_uri = flow.step1_get_authorize_url()
return flask.redirect(auth_uri)
else:
auth_code = flask.request.args.get('code')
credentials = flow.step2_exchange(auth_code)
session['credentials'] = credentials.to_json()
return flask.redirect(flask.url_for('station'))
那么会发生什么:Firts浏览器呈现index.html。没问题,用户点击了一些内容并将GET参数传递给了电台()。它通过:
if 'st' not in session
正确设置了会话密钥。我在印刷品中看到了它。然后用户被重定向到/ oauth2callback。使用youtube api进行整个身份验证过程,当脚本将用户从/ oauth2callback返回到/ station时
return flask.redirect(flask.url_for('station'))
由于某种原因代码进入
if 'st' not in session
再次。就像没有会话[' st']了。在结果中,我没有从表格中获得价值......
我不知道该怎么做。 (使用Flask 0.10并在Heroku上托管东西)