youtube api v3身份验证期间烧瓶会话出错

时间:2015-09-07 10:47:09

标签: python youtube flask youtube-api youtube-data-api

我正在开发一个使用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'))

所以在浏览器中我进行身份验证,接受应用程序的权限,然后我看到500个内部错误,带有ouath2callback URI和代码参数。在错误日志中,我看到了:

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会话可能有问题 - 也许是激情类型? 使用Flask 0.10

请问任何想法?

1 个答案:

答案 0 :(得分:1)

要使用sessions,您需要设置SECRET_KEY。

摘自docs

# set the secret key.  keep this really secret:
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'