Async OAuth Client for HTTPX

0.16.1 · active · verified Mon Apr 13

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a GitHub OAuth2 client and generate an authorization URL. It uses environment variables for client credentials, which is recommended for security. In a full application, you would capture the authorization code from the redirect URI and exchange it for an access token, then potentially fetch user profile information.

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())

view raw JSON →