Async OAuth Client for HTTPX
httpx-oauth is an asynchronous OAuth client library built on top of HTTPX. It provides clients for various OAuth2 providers (GitHub, Google, Facebook, etc.) as well as a generic OAuth2 client. The library is actively maintained with a regular release cadence, including minor versions introducing new features and breaking changes, and patch versions for bug fixes and dependency updates. Current version is 0.16.1.
Warnings
- breaking Python 3.8 support was dropped in `v0.16.0`. Users on Python 3.8 or older must upgrade their Python version or stay on `httpx-oauth<0.16.0`.
- breaking All exception classes were moved from `httpx_oauth.oauth2` (and similar modules) to `httpx_oauth.exceptions`. Additionally, these exceptions now require a `message` property during instantiation.
- gotcha Older versions of `httpx-oauth` had strict upper bound constraints on `httpx` (e.g., `<0.28`). If you are upgrading `httpx` but not `httpx-oauth`, you might encounter dependency conflicts. `v0.16.1` removed the upper bound, allowing it to work with newer `httpx` versions.
- gotcha The `get_profile` method on OAuth2 clients (e.g., `GitHubOAuth2.get_profile`) was introduced in `v0.16.0`. Attempts to use this method on earlier versions will result in an `AttributeError`.
Install
-
pip install httpx-oauth
Imports
- GitHubOAuth2
from httpx_oauth.clients.github import GitHubOAuth2
- GoogleOAuth2
from httpx_oauth.clients.google import GoogleOAuth2
- OAuth2
from httpx_oauth.oauth2 import OAuth2
- GetAccessTokenError
from httpx_oauth.exceptions import GetAccessTokenError
Quickstart
import asyncio
import os
from httpx_oauth.clients.github import GitHubOAuth2
async def main():
CLIENT_ID = os.environ.get('GITHUB_CLIENT_ID', 'your_github_client_id')
CLIENT_SECRET = os.environ.get('GITHUB_CLIENT_SECRET', 'your_github_client_secret')
REDIRECT_URI = 'http://localhost:8000/callback'
client = GitHubOAuth2(CLIENT_ID, CLIENT_SECRET)
authorization_url = await client.get_authorization_url(
redirect_uri=REDIRECT_URI,
scope=['user:email']
)
print(f"Please visit this URL to authorize: {authorization_url}")
# In a real application, you would handle the redirect from GitHub
# and then exchange the code for an access token.
# For demonstration, we'll simulate a code.
# code = "<CODE_FROM_REDIRECT_URL>"
# token_data = await client.get_access_token(code, REDIRECT_URI)
# print("Access Token Data:", token_data)
# Example of fetching user profile (requires access token)
# if 'access_token' in token_data:
# user_profile = await client.get_profile(token_data['access_token'])
# print("User Profile:", user_profile)
if __name__ == '__main__':
asyncio.run(main())