Registry /
type-stubs / types-requests-oauthlib
Install & Compatibility
Where this runs
tested against v2.0.0.20260610 · 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.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 71.9MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 2.1s · import 0.000s · 22MB
45MB installed
● package 45MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
OAuth1Session
✓ from requests_oauthlib_stubs import OAuth1Session
✗ from requests_oauthlib_stubs import OAuth1Session
This quickstart demonstrates a basic OAuth 2.0 Authorization Code flow using `requests-oauthlib` with static type checking provided by `types-requests-oauthlib`. It covers initializing an `OAuth2Session`, generating an authorization URL, simulating a callback, fetching an access token, and making a protected API call. Type hints are explicitly added to illustrate how the stub package aids in catching potential type mismatches during development.
import os
from typing import Dict, Any, Tuple
from requests_oauthlib import OAuth2Session
from requests.models import Response
# --- Configuration (replace with your actual values or env vars) ---
CLIENT_ID: str = os.environ.get('OAUTH_CLIENT_ID', 'your_client_id')
CLIENT_SECRET: str = os.environ.get('OAUTH_CLIENT_SECRET', 'your_client_secret')
AUTHORIZATION_BASE_URL: str = os.environ.get('OAUTH_AUTH_URL', 'https://example.com/oauth/authorize')
TOKEN_URL: str = os.environ.get('OAUTH_TOKEN_URL', 'https://example.com/oauth/token')
REDIRECT_URI: str = os.environ.get('OAUTH_REDIRECT_URI', 'https://example.com/callback')
SCOPE: list[str] = ['read', 'write'] # Example scopes
def run_oauth_flow() -> None:
# 1. Create an OAuth2Session instance
# The type hints from types-requests-oauthlib will check this.
oauth: OAuth2Session = OAuth2Session(CLIENT_ID, redirect_uri=REDIRECT_URI, scope=SCOPE)
# 2. Prepare the authorization request URL
authorization_url: str
state: str
authorization_url, state = oauth.authorization_url(AUTHORIZATION_BASE_URL)
print(f"Visit this URL to authorize: {authorization_url}")
print(f"Remember state for CSRF protection: {state}")
# --- Simulate authorization response ---
# In a real application, the user would visit authorization_url, grant access,
# and be redirected to REDIRECT_URI with a 'code' and 'state' in the URL.
# For this example, we'll simulate the response URL.
simulated_auth_response_url: str = f"{REDIRECT_URI}?code=AUTHORIZATION_CODE_FROM_PROVIDER&state={state}"
print(f"\nSimulating callback URL: {simulated_auth_response_url}")
# 3. Fetch the access token
token: Dict[str, Any] = oauth.fetch_token(
TOKEN_URL,
client_secret=CLIENT_SECRET,
authorization_response=simulated_auth_response_url
)
print(f"\nAccess Token obtained: {token}")
# 4. Use the obtained token to make a protected API request
# The 'oauth' session automatically adds the authorization header.
# The type hints for 'get' and 'Response' are provided by types-requests and types-requests-oauthlib.
try:
response: Response = oauth.get('https://example.com/api/protected_resource')
response.raise_for_status() # Raise an exception for HTTP errors
print(f"\nProtected resource content: {response.json()}")
except Exception as e:
print(f"\nError accessing protected resource: {e}")
if __name__ == "__main__":
run_oauth_flow()
Debug
Known issues
gotchaThe `types-requests-oauthlib` package only provides static type hints (stubs). It does NOT include the actual runtime code for `requests-oauthlib`. You must install `requests-oauthlib` separately for your application to function at runtime.fixAlways install both `requests-oauthlib` and `types-requests-oauthlib`: `pip install requests-oauthlib types-requests-oauthlib`.
affects: All versions of `types-requests-oauthlib`
breakingType stub versions are explicitly tied to the runtime package versions they annotate. `types-requests-oauthlib` v2.0.0.20260408 provides annotations for `requests-oauthlib==2.0.*`. Using a stub package version that is incompatible with your runtime `requests-oauthlib` version can lead to incorrect type checking or errors. Minor updates to stub packages can also sometimes introduce new type errors, as typeshed aims for strictness.fixPin your `types-requests-oauthlib` version to match the expected runtime version (e.g., `types-requests-oauthlib~=2.0.0`) or use a version range that ensures compatibility. Regularly update both the runtime and stub packages together after verifying compatibility.
affects: All versions
gotcha`types-requests-oauthlib` does not add or alter runtime functionality, fix bugs in `requests-oauthlib`, or change its behavior. Its sole purpose is for static analysis by type checkers. Expecting it to resolve runtime issues is a common misunderstanding.fixUnderstand that stub packages are for compile-time type checking only. Any runtime issues or feature requests should be directed to the `requests-oauthlib` project.
affects: All versions
deprecatedThe `types-requests-oauthlib` package requires Python 3.10 or newer. Older Python versions (e.g., 3.9 and below) are not supported by recent versions of this stub package.fixEnsure your project uses Python 3.10 or a more recent version. If you must use an older Python version, you may need to find an older, compatible version of `types-requests-oauthlib` (if one exists) or disable type checking for this library.
affects: Versions 2.0.0.x and newer
Upgrade
Version history
2.0.0.20260610latest on PyPI · released Jun 10, 2026
Audit
Dependencies
requests-oauthlibrequiredThis package provides typing stubs for `requests-oauthlib`, which must be installed for runtime functionality.
requestsrequired`requests-oauthlib` depends on the `requests` library.
oauthlibrequired`requests-oauthlib` builds upon the `oauthlib` library.