23. May 2015

Flask OAuthlib Multiple Scope Values for calling Google API

Google provides myriad of APIs for invoking operations on Google App Platform. It’s possible to integrate this calls with custom app using OAuth.

One option is to write app based on Flask (Python Microframework) with OAuth support provided by Flask-OAuthlib.

There is simple example of web app in Lepture’s repo.

The key practice in OAuth world is to get user’s consent to access API on her/his behalf. Often implemented by simple consent screen.

google-consent

You need to perform two steps to display consent screen:

  • enable API in Developer Console
  • define scope in your application

The second step is straightdorward:

google = oauth.remote_app(
    'google',
    ...
    request_token_params={
        'scope': 'https://www.googleapis.com/auth/userinfo.email'
    },
    ...
)

It works perfectly. The only problem is that this solution provides access just to one API.

The question is: How to request access to multiple scopes?

You can find many hints about OAtuh for other frameworks, that you should separate scopes by comma. That won’t work.

Correct solution is to use white space as delimiter of scopes (as suggested for HTML forms).

google = oauth.remote_app(
    'google',
    ...
    request_token_params={
        'scope': 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/calendar.readonly https://www.googleapis.com/auth/tasks'
    },
    ...
)