{"id":5539,"library":"types-requests-oauthlib","title":"Typing stubs for requests-oauthlib","description":"This package provides static typing stubs for the `requests-oauthlib` library, enabling type checkers like Mypy and Pyright to perform static analysis on code that uses `requests-oauthlib`. It is part of the broader typeshed project, which maintains a collection of high-quality type annotations for Python libraries. The current version, 2.0.0.20260408, aims to provide accurate annotations for `requests-oauthlib==2.0.*` and requires Python >=3.10. Stub packages from typeshed are released automatically when changes are merged into the typeshed repository.","status":"active","version":"2.0.0.20260408","language":"en","source_language":"en","source_url":"https://github.com/python/typeshed/tree/main/stubs/requests-oauthlib","tags":["types","type hints","stubs","requests-oauthlib","oauth","requests","typeshed"],"install":[{"cmd":"pip install types-requests-oauthlib","lang":"bash","label":"Install stubs"},{"cmd":"pip install requests-oauthlib","lang":"bash","label":"Install runtime library (required)"}],"dependencies":[{"reason":"This package provides typing stubs for `requests-oauthlib`, which must be installed for runtime functionality.","package":"requests-oauthlib","optional":false},{"reason":"`requests-oauthlib` depends on the `requests` library.","package":"requests","optional":false},{"reason":"`requests-oauthlib` builds upon the `oauthlib` library.","package":"oauthlib","optional":false}],"imports":[{"note":"`types-requests-oauthlib` provides type hints for this symbol from the runtime `requests-oauthlib` library.","symbol":"OAuth1Session","correct":"from requests_oauthlib import OAuth1Session"},{"note":"`types-requests-oauthlib` provides type hints for this symbol from the runtime `requests-oauthlib` library.","symbol":"OAuth2Session","correct":"from requests_oauthlib import OAuth2Session"},{"note":"`types-requests-oauthlib` provides type hints for this symbol from the runtime `requests-oauthlib` library.","symbol":"OAuth1","correct":"from requests_oauthlib import OAuth1"}],"quickstart":{"code":"import os\nfrom typing import Dict, Any, Tuple\nfrom requests_oauthlib import OAuth2Session\nfrom requests.models import Response\n\n# --- Configuration (replace with your actual values or env vars) ---\nCLIENT_ID: str = os.environ.get('OAUTH_CLIENT_ID', 'your_client_id')\nCLIENT_SECRET: str = os.environ.get('OAUTH_CLIENT_SECRET', 'your_client_secret')\nAUTHORIZATION_BASE_URL: str = os.environ.get('OAUTH_AUTH_URL', 'https://example.com/oauth/authorize')\nTOKEN_URL: str = os.environ.get('OAUTH_TOKEN_URL', 'https://example.com/oauth/token')\nREDIRECT_URI: str = os.environ.get('OAUTH_REDIRECT_URI', 'https://example.com/callback')\nSCOPE: list[str] = ['read', 'write'] # Example scopes\n\ndef run_oauth_flow() -> None:\n    # 1. Create an OAuth2Session instance\n    # The type hints from types-requests-oauthlib will check this.\n    oauth: OAuth2Session = OAuth2Session(CLIENT_ID, redirect_uri=REDIRECT_URI, scope=SCOPE)\n\n    # 2. Prepare the authorization request URL\n    authorization_url: str\n    state: str\n    authorization_url, state = oauth.authorization_url(AUTHORIZATION_BASE_URL)\n\n    print(f\"Visit this URL to authorize: {authorization_url}\")\n    print(f\"Remember state for CSRF protection: {state}\")\n\n    # --- Simulate authorization response ---\n    # In a real application, the user would visit authorization_url, grant access,\n    # and be redirected to REDIRECT_URI with a 'code' and 'state' in the URL.\n    # For this example, we'll simulate the response URL.\n    simulated_auth_response_url: str = f\"{REDIRECT_URI}?code=AUTHORIZATION_CODE_FROM_PROVIDER&state={state}\"\n    print(f\"\\nSimulating callback URL: {simulated_auth_response_url}\")\n\n    # 3. Fetch the access token\n    token: Dict[str, Any] = oauth.fetch_token(\n        TOKEN_URL,\n        client_secret=CLIENT_SECRET,\n        authorization_response=simulated_auth_response_url\n    )\n\n    print(f\"\\nAccess Token obtained: {token}\")\n\n    # 4. Use the obtained token to make a protected API request\n    # The 'oauth' session automatically adds the authorization header.\n    # The type hints for 'get' and 'Response' are provided by types-requests and types-requests-oauthlib.\n    try:\n        response: Response = oauth.get('https://example.com/api/protected_resource')\n        response.raise_for_status() # Raise an exception for HTTP errors\n        print(f\"\\nProtected resource content: {response.json()}\")\n    except Exception as e:\n        print(f\"\\nError accessing protected resource: {e}\")\n\nif __name__ == \"__main__\":\n    run_oauth_flow()","lang":"python","description":"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."},"warnings":[{"fix":"Always install both `requests-oauthlib` and `types-requests-oauthlib`: `pip install requests-oauthlib types-requests-oauthlib`.","message":"The `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.","severity":"gotcha","affected_versions":"All versions of `types-requests-oauthlib`"},{"fix":"Pin 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.","message":"Type 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.","severity":"breaking","affected_versions":"All versions"},{"fix":"Understand that stub packages are for compile-time type checking only. Any runtime issues or feature requests should be directed to the `requests-oauthlib` project.","message":"`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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure 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.","message":"The `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.","severity":"deprecated","affected_versions":"Versions 2.0.0.x and newer"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}