OAuth2 Client

raw JSON →
1.4.2 verified Mon Apr 27 auth: no python

A client library for OAuth2. Version 1.4.2, released irregularly. Provides OAuth2 authentication flows for Python applications.

pip install oauth2-client
error ImportError: No module named oauth2_client
cause Trying to import 'oauth2_client' (with underscore) instead of 'oauth2client' (no underscore).
fix
from oauth2client.client import OAuth2Client
error AttributeError: module 'oauth2client' has no attribute 'OAuth2Client'
cause Trying to import from the top-level package instead of the 'client' submodule.
fix
Use 'from oauth2client.client import OAuth2Client'.
error KeyError: 'access_token'
cause OAuth2 provider response missing the required 'access_token' field.
fix
Check provider's token endpoint response and ensure it contains 'access_token'.
gotcha Package name 'oauth2-client' imports as 'oauth2client' (no hyphen). Many users try 'import oauth2_client' which fails.
fix Use 'from oauth2client.client import OAuth2Client'.
deprecated The 'oauth2-client' library is often confused with google-auth-oauthlib. This library is not maintained by Google and may lack modern features.
fix Consider using 'requests-oauthlib' or 'google-auth-oauthlib' for better maintained OAuth2 client.
breaking In version 1.3.0, the token endpoint response handling changed: the 'access_token' key is now required. Missing keys raise KeyError.
fix Ensure provider returns an 'access_token' field in the JSON response.

Create an OAuth2 client with environment variables for credentials.

from oauth2client.client import OAuth2Client
import os

client_id = os.environ.get('OAUTH2_CLIENT_ID', '')
client_secret = os.environ.get('OAUTH2_CLIENT_SECRET', '')
redirect_uri = 'http://localhost:8000/callback'

client = OAuth2Client(
    client_id=client_id,
    client_secret=client_secret,
    redirect_uris=[redirect_uri],
    auth_uri='https://provider.com/auth',
    token_uri='https://provider.com/token',
    scope=['email', 'profile']
)
print('Client created:', client.client_id)