OAuthLib: OAuth1 and OAuth2 Implementation for Python

raw JSON →
3.3.1 verified Tue May 12 auth: no python install: stale quickstart: stale

OAuthLib is a comprehensive, spec-compliant implementation of OAuth1 and OAuth2 request-signing logic for Python 3.8 and above. The current version is 3.3.1, released on March 27, 2026, with a regular release cadence for ongoing improvements and security updates.

pip install oauthlib
error oauthlib.oauth2.rfc6749.errors.MismatchingStateError: (mismatching_state) CSRF Warning! State not equal in request and response.
cause This error typically occurs in OAuth2 flows when the 'state' parameter, used for CSRF protection, sent in the initial authorization request does not match the 'state' parameter received in the callback response from the OAuth provider. This can be due to session issues (e.g., session not being saved or loaded correctly, especially across redirects or in production environments with misconfigured sessions), or issues with how the state is handled by the application or framework.
fix
Ensure that your web framework's session management is correctly configured and working across redirects (e.g., Flask sessions, Django sessions). Verify that the state parameter is consistently stored in the session before redirection to the OAuth provider and retrieved/validated upon callback. In Flask, ensure client_id and other sensitive configurations are loaded within the application context, not at module level, to prevent None values.
error oauthlib.oauth2.rfc6749.errors.InsecureTransportError: (insecure_transport) OAuth 2 MUST utilize https.
cause OAuth 2.0 strictly requires the use of HTTPS for all communication to ensure security. This error arises when an OAuth2 request is attempted over an insecure HTTP connection, often during local development or when a reverse proxy (like Nginx or Cloudflare) terminates SSL but the application itself perceives the request as HTTP.
fix
For local development, you can temporarily disable this check by setting the environment variable OAUTHLIB_INSECURE_TRANSPORT=1 or by adding os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' in your Python code before making OAuth requests (never in production). For production behind a reverse proxy, ensure the proxy is correctly configured to forward the X-Forwarded-Proto header as https and that your web framework recognizes it.
error oauthlib.oauth2.rfc6749.errors.InvalidGrantError: (invalid_grant) The user could not be authenticated as the grant is expired.
cause This error indicates that the authorization grant (e.g., authorization code or refresh token) provided is invalid, expired, or has been used previously. Authorization codes are typically short-lived and single-use, while refresh tokens can also expire or be revoked.
fix
Ensure that authorization codes are exchanged for access/refresh tokens immediately after receipt and are not reused. If using refresh tokens, implement robust handling to acquire new ones when they expire and store them securely, making sure to discard old refresh tokens as they can often only be used once. Re-authenticate the user if the refresh token is no longer valid.
error ModuleNotFoundError: No module named 'google_auth_oauthlib'
cause This is a common Python error indicating that the `google_auth_oauthlib` package, which leverages `oauthlib` for Google's OAuth 2.0 authentication flow, is not installed or not accessible in the current Python environment.
fix
Install the google-auth-oauthlib package using pip: pip install google-auth-oauthlib. Ensure that you are installing it into the correct Python environment (e.g., your active virtual environment) and that the Python interpreter running your code is the one associated with that environment.
breaking OAuthLib 3.0.0 introduced API-breaking changes, including the relocation of 'OAuth1Session' (e.g., to 'oauthlib.oauth1.rfc5849') and the removal of 'request' from 'confirm_redirect_uri'.
fix Update your import statements for 'OAuth1Session' (e.g., from 'oauthlib.oauth1' to 'oauthlib.oauth1.rfc5849'). Also, align other parts of your code with the new API changes introduced in version 3.0.0, referring to OAuthLib release notes and documentation.
gotcha Ensure that 'requests' is installed when using OAuthLib, as it is commonly used for making HTTP requests in Python applications.
fix Install the 'requests' library using 'pip install requests' if it's not already installed.
breaking The OAuth1Session class was relocated in OAuthLib version 3.0.0. It is no longer directly available under `oauthlib.oauth1` and must be imported from `oauthlib.oauth1.rfc5849` or `requests_oauthlib`.
fix Update your import statement. If using `requests-oauthlib`, change `from oauthlib.oauth1 import OAuth1Session` to `from requests_oauthlib import OAuth1Session` (and ensure `requests-oauthlib` is installed). If using OAuthLib directly, change it to `from oauthlib.oauth1.rfc5849 import OAuth1Session`.
python os / libc status wheel install import disk
3.10 alpine (musl) - - - -
3.10 slim (glibc) - - - -
3.11 alpine (musl) - - - -
3.11 slim (glibc) - - - -
3.12 alpine (musl) - - - -
3.12 slim (glibc) - - - -
3.13 alpine (musl) - - - -
3.13 slim (glibc) - - - -
3.9 alpine (musl) - - - -
3.9 slim (glibc) - - - -

A basic example of using OAuthLib with OAuth1Session to make authenticated requests.

import os
from oauthlib.oauth1 import OAuth1Session

# Set up OAuth1Session with your credentials
oauth = OAuth1Session(client_key=os.environ.get('CLIENT_KEY'), client_secret=os.environ.get('CLIENT_SECRET'))

# Make a request to a protected resource
response = oauth.get('https://api.example.com/protected')
print(response.content)