This question already has answers here:
demystify Flask app.secret_key
(2个答案)
5年前关闭。
我正在开发使用YT API的python应用程序。我决定将其移动到网络上,而我正在使用Flask。我以Google指南中的auth为例。出于测试目的,我尝试在身份验证后创建播放列表。看起来像这样:
因此,在浏览器中,我进行身份验证,接受应用程序的权限,然后看到ouath2callback URI和代码参数出现500个内部错误。在错误日志中,我看到以下内容:
我认为Flask session 可能有问题-可能是seassion类型?
使用Flask 0.10
有什么想法吗?
(2个答案)
5年前关闭。
我正在开发使用YT API的python应用程序。我决定将其移动到网络上,而我正在使用Flask。我以Google指南中的auth为例。出于测试目的,我尝试在身份验证后创建播放列表。看起来像这样:
@app.route('/')
def index():
if 'credentials' not in flask.session:
return flask.redirect(flask.url_for('oauth2callback'))
credentials = client.OAuth2Credentials.from_json(flask.session['credentials'])
if credentials.access_token_expired:
return flask.redirect(flask.url_for('oauth2callback'))
else:
http_auth = credentials.authorize(httplib2.Http())
yt_service = discovery.build('youtube', 'v3', http_auth)
playlists_insert_response = yt_service.playlists().insert(
part="snippet,status",
body=dict(
snippet=dict(
title="Test Playlist",
description="A private playlist created with the YouTube API v3"
),
status=dict(
privacyStatus="private"
)
)
).execute()
return playlists_insert_response["id"]
@app.route('/oauth2callback')
def oauth2callback():
flow = client.flow_from_clientsecrets('youtube/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)
flask.session['credentials'] = credentials.to_json()
return flask.redirect(flask.url_for('index'))
因此,在浏览器中,我进行身份验证,接受应用程序的权限,然后看到ouath2callback URI和代码参数出现500个内部错误。在错误日志中,我看到以下内容:
2015-09-07 10:23:08,319 :Successfully retrieved access token
2015-09-07 10:23:08,330 :Exception on /oauth2callback [GET]
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1360, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1358, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1344, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/Kraxi/youtube/playlist.py", line 63, in oauth2callback
flask.session['credentials'] = credentials.to_json()
File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 339, in __setitem__
self._get_current_object()[key] = value
File "/usr/local/lib/python2.7/dist-packages/flask/sessions.py", line 57, in _fail
raise RuntimeError('the session is unavailable because no secret '
RuntimeError: the session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.
我认为Flask session 可能有问题-可能是seassion类型?
使用Flask 0.10
有什么想法吗?
最佳答案
为了使用sessions
,您需要设置SECRET_KEY。
从docs中提取:
# set the secret key. keep this really secret:
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'