0

I'm developing an app in python that uses YT API. I decided to move it to the web and i'm using Flask. I took the auth example from Google guides. For testing purposes i'm trying to create a playlist after authentication. It looks like this:

@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'))

So in browser I authenticate, accept app's permisions and then I see 500 internal error with ouath2callback URI and code parameter. In the error log I see this:

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.

I tihnk it might be something wrong with Flask session - maybe seassion type? Using Flask 0.10

Any ideas, please?

Kraxi
  • 93
  • 2
  • 12

1 Answers1

1

In order to use sessions you need to set a SECRET_KEY.

Extracted from the docs:

# set the secret key.  keep this really secret:
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
Paolo Casciello
  • 7,982
  • 1
  • 43
  • 42