HTTPX Auth
httpx-auth is a Python library that provides a collection of authentication classes designed for use with the HTTPX client library. It supports various authentication schemes, including OAuth2 (Authorization Code, PKCE, Client Credentials, Resource Owner Password Credentials, Implicit flows), Okta, Microsoft Entra ID (formerly Azure AD), and AWS Signature Version 4. While a 1.0.0 release is pending HTTPX's own 1.0.0, the library is considered stable and is actively maintained.
Warnings
- breaking When providing an `httpx.Client` instance as a parameter to any `httpx-auth` OAuth2 authentication class (e.g., `client` parameter in `OAuth2AuthorizationCode`), `httpx-auth` no longer closes this client automatically. Users are now responsible for explicitly closing these client instances when they are no longer needed to prevent resource leaks.
- gotcha The `AWS4Auth` class, ported from `requests-aws4auth`, has specific behavioral changes and deprecated attributes compared to its origin. Notably, the `amz_date` attribute has been removed, direct provision of `AWSSigningKey` instances is not supported (use explicit parameters instead), and the `date` parameter now defaults to `now()` without options to override or raise on invalid date.
Install
-
pip install httpx-auth
Imports
- OAuth2AuthorizationCode
from httpx_auth import OAuth2AuthorizationCode
- AWS4Auth
from httpx_auth import AWS4Auth
- OktaAuthorizationCodePKCE
from httpx_auth import OktaAuthorizationCodePKCE
- MicrosoftEntraID
from httpx_auth import MicrosoftEntraID
Quickstart
import httpx
import os
from httpx_auth import OAuth2AuthorizationCode
# In a real application, these would come from environment variables or a secure configuration
# For this example, we use placeholders. Replace with your actual OAuth2 application details.
CLIENT_ID = os.environ.get("OAUTH_CLIENT_ID", "your_client_id")
AUTHORIZATION_URL = os.environ.get("OAUTH_AUTH_URL", "https://example.com/oauth/authorize")
TOKEN_URL = os.environ.get("OAUTH_TOKEN_URL", "https://example.com/oauth/token")
try:
auth = OAuth2AuthorizationCode(
client_id=CLIENT_ID,
authorization_url=AUTHORIZATION_URL,
token_url=TOKEN_URL,
# For local development, redirect_uri would typically be a local callback URL.
# httpx-auth will start a local server to capture the redirect.
# Ensure this matches what's configured for your OAuth2 client application.
# Example: redirect_uri="http://localhost:8000/callback", port=8000
)
with httpx.Client() as client:
# The first request will trigger the OAuth2 flow:
# 1. Opens a browser for user consent.
# 2. User grants permission, browser redirects to redirect_uri.
# 3. httpx-auth captures the code and exchanges it for a token.
# 4. The request to the protected resource is then made with the acquired token.
response = client.get("https://api.example.com/protected-resource", auth=auth)
response.raise_for_status()
print(f"Successfully authenticated and fetched data: {response.json()}")
except httpx.HTTPStatusError as e:
print(f"HTTP error occurred: {e.response.status_code} - {e.response.text}")
except httpx.RequestError as e:
print(f"An error occurred while making the request: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")