Install & Compatibility
Where this runs
tested against v0.9.6 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 27.6MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 3.1s · import 0.000s · 28MB
26MB installed
● package 26MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
OAuth
✓ from flask_oauthlib.client import OAuth
✗ from flask_oauthlib.client import OAuth
OAuthRemoteApp
✓ from flask_oauthlib.client import OAuthRemoteApp
OAuthProvider
✓ from flask_oauthlib.provider import OAuthProvider
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)
Upgrade
Version history
0.9.6latest on PyPI · released Sep 7, 2020
Audit
Dependencies
oauthlibrequiredCore OAuth protocol implementation. Known to have version conflicts with Flask-OAuthlib.
FlaskrequiredThe web framework it extends.