Flask-GoogleLogin

Flask-GoogleLogin extends Flask-Login to use Google OAuth2’s authorization.

Usage

Get started by creating a GoogleLogin instance:

from flask_googlelogin import GoogleLogin
googlelogin = GoogleLogin(app)
# or
googlelogin = GoogleLogin()
googlelogin.init_app(app)

A flask_login.LoginManager instance is implicitly created in GoogleLogin.init_app but you can also pass your own:

from flask_login import LoginManager
login_manager = LoginManager()
login_manager.init_app(app)
googlelogin = GoogleLogin(app, login_manager)

Next, you need to specify an OAuth2 callback route:

@app.route('/oauth2callback')
@googlelogin.oauth2callback
def create_or_update_user(token, userinfo, **params):
    user = User.filter_by(google_id=userinfo['id']).first()
    if user:
        user.name = userinfo['name']
        user.avatar = userinfo['picture']
    else:
        user = User(google_id=userinfo['id'],
                    name=userinfo['name'],
                    avatar=userinfo['picture'])
    db.session.add(user)
    db.session.flush()
    login_user(user)
    return redirect(url_for('index'))

Decorate views with flask_login.login_required and you’re done!

@app.route('/profile')
@login_required
def profile():
    return render_template('profile.html')

Get the Google auth URL using GoogleLogin.login_url. You can also include extra params to login_url and they’ll be passed to your GoogleLogin.oauth2callback:

googlelogin.login_url(params=dict(section='notifications', next=url_for('.profile')))

You can also configure Google auth url params:

googlelogin.login_url(approval_prompt='force', access_type='offline')
googlelogin.login_url(redirect_uri=url_for('admin'))

Configuration

Google API

GOOGLE_LOGIN_CLIENT_ID Client ID (create one at https://code.google.com/apis/console)
GOOGLE_LOGIN_CLIENT_SECRET Client Secret
GOOGLE_LOGIN_CLIENT_SCOPES Default scopes
GOOGLE_LOGIN_REDIRECT_URI Default redirect URI

API

class flask_googlelogin.GoogleLogin(app=None, login_manager=None)[source]

Main extension class

init_app(app, add_context_processor=True, login_manager=None)[source]

Initialize with app configuration. Existing flask_login.LoginManager instance can be passed.

login_url(params=None, **kwargs)[source]

Return login url with params encoded in state

Available Google auth server params: prompt: none, select_account, consent approval_prompt: force, auto access_type: online, offline scopes: string (separated with commas) or list redirect_uri: string login_hint: string

unauthorized_callback()[source]

Redirect to login url with next param set as request.url

login(code, redirect_uri)[source]

Exchanges code for tokens and returns retrieved userinfo and token

get_access_token(refresh_token)[source]

Use a refresh token to obtain a new access token

oauth2callback(view_func)[source]

Decorator for OAuth2 callback. Calls GoogleLogin.login then passes results to view_func.

user_loader(func)[source]

Shortcut to login_manager‘s flask_login.LoginManager.user_loader

Fork me on GitHub