{"library":"requests-auth","title":"requests-auth","description":"requests-auth provides a collection of authentication classes that extend the capabilities of the popular Python `requests` library. It simplifies the implementation of various authentication schemes like OAuth2 (Authorization Code, Client Credentials, PKCE), Okta, Microsoft Entra ID (formerly Azure Active Directory), API Key, Basic, and NTLM. The current version is 8.0.0, released on June 18, 2024, and it typically follows the release cadence of its underlying `requests` and `requests-oauthlib` dependencies, with new features and bug fixes released as needed.","language":"python","status":"active","last_verified":"Sun May 17","install":{"commands":["pip install requests-auth"],"cli":null},"imports":["from requests_auth import OAuth2AuthorizationCode","from requests_auth import OAuth2ClientCredentials","from requests_auth import ApiKeyAuth","from requests_auth.authentication import BearerTokenAuth","from requests_auth import OktaAuthorizationCode","from requests_auth import MicrosoftAuthorizationCode"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import requests\nimport os\nfrom requests_auth import OAuth2ClientCredentials\n\n# --- Client Credentials Flow Example ---\n# Replace with your actual client_id, client_secret, token_url, and API endpoint\nCLIENT_ID = os.environ.get('OAUTH_CLIENT_ID', 'your_client_id')\nCLIENT_SECRET = os.environ.get('OAUTH_CLIENT_SECRET', 'your_client_secret')\nTOKEN_URL = os.environ.get('OAUTH_TOKEN_URL', 'https://example.com/oauth/token')\nAPI_ENDPOINT = os.environ.get('API_ENDPOINT', 'https://api.example.com/data')\n\nif CLIENT_ID and CLIENT_SECRET and TOKEN_URL and API_ENDPOINT:\n    try:\n        # Initialize OAuth2ClientCredentials auth\n        auth = OAuth2ClientCredentials(\n            token_url=TOKEN_URL,\n            client_id=CLIENT_ID,\n            client_secret=CLIENT_SECRET\n        )\n\n        # Make an authenticated request\n        response = requests.get(API_ENDPOINT, auth=auth)\n        response.raise_for_status() # Raise an exception for HTTP errors\n        print(f\"Client Credentials API Response: {response.json()}\")\n    except requests.exceptions.HTTPError as e:\n        print(f\"Client Credentials HTTP Error: {e.response.status_code} - {e.response.text}\")\n    except Exception as e:\n        print(f\"An error occurred during Client Credentials flow: {e}\")\nelse:\n    print(\"Skipping Client Credentials example: Environment variables for client ID, secret, token URL, or API endpoint are not set.\")\n\n# --- API Key in Header Example ---\n# Replace with your actual API key and endpoint\nAPI_KEY_HEADER = os.environ.get('API_KEY_HEADER', 'your_api_key_value')\nAPI_KEY_HEADER_NAME = 'X-API-Key'\nAPI_ENDPOINT_HEADER = os.environ.get('API_ENDPOINT_HEADER', 'https://api.example.com/header-data')\n\nif API_KEY_HEADER and API_ENDPOINT_HEADER:\n    try:\n        # Initialize ApiKeyAuth for header\n        auth_header = ApiKeyAuth(\n            API_KEY_HEADER, \n            API_KEY_HEADER_NAME, \n            'header'\n        )\n\n        response_header = requests.get(API_ENDPOINT_HEADER, auth=auth_header)\n        response_header.raise_for_status()\n        print(f\"API Key (Header) API Response: {response_header.json()}\")\n    except requests.exceptions.HTTPError as e:\n        print(f\"API Key (Header) HTTP Error: {e.response.status_code} - {e.response.text}\")\n    except Exception as e:\n        print(f\"An error occurred during API Key (Header) example: {e}\")\nelse:\n    print(\"Skipping API Key (Header) example: Environment variables for API key or endpoint are not set.\")\n\n# --- Bearer Token Auth Example (using a pre-obtained token) ---\n# In a real application, this token would typically come from an OAuth2 flow.\nBEARER_TOKEN = os.environ.get('BEARER_TOKEN', 'your_bearer_token')\nAPI_ENDPOINT_BEARER = os.environ.get('API_ENDPOINT_BEARER', 'https://api.example.com/bearer-data')\n\nif BEARER_TOKEN and API_ENDPOINT_BEARER:\n    try:\n        # Initialize BearerTokenAuth\n        bearer_auth = BearerTokenAuth(BEARER_TOKEN)\n\n        response_bearer = requests.get(API_ENDPOINT_BEARER, auth=bearer_auth)\n        response_bearer.raise_for_status()\n        print(f\"Bearer Token API Response: {response_bearer.json()}\")\n    except requests.exceptions.HTTPError as e:\n        print(f\"Bearer Token HTTP Error: {e.response.status_code} - {e.response.text}\")\n    except Exception as e:\n        print(f\"An error occurred during Bearer Token example: {e}\")\nelse:\n    print(\"Skipping Bearer Token example: Environment variables for bearer token or endpoint are not set.\")","lang":"python","description":"This quickstart demonstrates how to use `requests-auth` for three common authentication patterns: OAuth2 Client Credentials flow, API Key in a header, and Bearer Token authentication. It retrieves sensitive credentials from environment variables for security. Ensure you replace placeholder URLs and environment variable names with your actual API details.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-17","installed_version":"8.0.0","pypi_latest":"8.0.0","is_stale":false,"summary":{"python_range":"3.10–3.9","success_rate":100,"avg_install_s":2.1,"avg_import_s":0.66,"wheel_type":"wheel"},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"requests-auth","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.64,"mem_mb":12.2,"disk_size":"21.5M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"requests-auth","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":2.1,"import_time_s":0.49,"mem_mb":12.2,"disk_size":"22M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"requests-auth","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.81,"mem_mb":13.4,"disk_size":"23.6M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"requests-auth","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":2.2,"import_time_s":0.7,"mem_mb":13.4,"disk_size":"24M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"requests-auth","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.73,"mem_mb":13.9,"disk_size":"15.4M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"requests-auth","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.9,"import_time_s":0.75,"mem_mb":13.9,"disk_size":"16M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"requests-auth","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.71,"mem_mb":14.3,"disk_size":"15.2M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"requests-auth","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":2,"import_time_s":0.7,"mem_mb":14.3,"disk_size":"16M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"requests-auth","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.59,"mem_mb":11.8,"disk_size":"20.8M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"requests-auth","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":2.5,"import_time_s":0.5,"mem_mb":11.8,"disk_size":"21M"}]}}