Flask-OAuthlib

0.9.6 · deprecated · verified Thu Apr 16

Flask-OAuthlib is an extension for Flask that provides both OAuth client and provider functionalities, built upon the `oauthlib` core. It supports OAuth 1.0a and OAuth 2.0. The library's last release was 0.9.6 in September 2020. It is officially deprecated and not actively maintained; users are strongly encouraged to migrate to `Authlib` for current and future projects.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic OAuth 2.0 client setup using `flask-oauthlib` to connect to a generic remote service. It configures a remote application, handles the authorization flow, stores the access token in the Flask session, and makes an example API call. Replace placeholder URLs and credentials with your actual OAuth provider details. Remember that `flask-oauthlib` is deprecated and this code serves mainly as a reference for existing implementations.

import os
from flask import Flask, redirect, url_for, session, request
from flask_oauthlib.client import OAuth

app = Flask(__name__)
app.debug = True
app.secret_key = 'development'

# NOTE: For local development over HTTP, you might need:
# os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'

# Configure your remote application details
# Replace with your actual consumer_key and consumer_secret
# These values should ideally come from environment variables for production
REMOTE_APP_CLIENT_ID = os.environ.get('REMOTE_APP_CLIENT_ID', 'your_client_id')
REMOTE_APP_CLIENT_SECRET = os.environ.get('REMOTE_APP_CLIENT_SECRET', 'your_client_secret')

# Initialize OAuth
oauth = OAuth(app)

remote_service = oauth.remote_app(
    'remote_service',
    consumer_key=REMOTE_APP_CLIENT_ID,
    consumer_secret=REMOTE_APP_CLIENT_SECRET,
    base_url='https://api.example.com/',
    request_token_url=None, # Not needed for OAuth2 client credentials or implicit grant
    request_token_params={'scope': 'email profile'},
    access_token_url='https://example.com/oauth/token',
    authorize_url='https://example.com/oauth/authorize',
    # Example using a tokengetter/tokensetter for persistent storage
    # In a real app, this would store tokens in a database associated with a user
    access_token_method='POST'
)

@remote_service.tokengetter
def get_remote_service_token():
    return session.get('remote_service_oauth_token')

@app.route('/')
def index():
    if 'remote_service_oauth_token' in session:
        resp = remote_service.get('userinfo') # Example API call
        return f'Logged in as {resp.data.get("email")}<br><a href="/logout">Logout</a>'
    return '<p>Hello! <a href="/login">Login with Remote Service</a></p>'

@app.route('/login')
def login():
    return remote_service.authorize(callback=url_for('authorized', _external=True))

@app.route('/logout')
def logout():
    session.pop('remote_service_oauth_token', None)
    return redirect(url_for('index'))

@app.route('/authorized')
def authorized():
    resp = remote_service.authorized_response()
    if resp is None or resp.get('access_token') is None:
        return f'Access denied: reason={request.args["error"]}, error={request.args["error_description"]}'
    session['remote_service_oauth_token'] = (resp['access_token'], '') # OAuth2 bearer token, secret is empty
    return redirect(url_for('index'))

if __name__ == '__main__':
    app.run(port=5000)

view raw JSON →