Skip to main content

AIOAuth Client

tests PyPI Python versions License: MIT

OAuth 1.0a and OAuth 2.0 client helpers for asyncio and trio, built on httpx.

  • OAuth 1.0a and OAuth 2.0 authorization flows
  • 28 ready-made provider clients (see the table)
  • works with asyncio and trio, no callbacks: plain async/await
  • typed, ships py.typed
  • Python 3.11+

Requirements

Python 3.11 or newer. The only runtime dependency is httpx.

Installation

pip install aioauth-client

Or with uv:

uv add aioauth-client

Usage

The snippets below are meant to run inside async code.

OAuth 2.0

from aioauth_client import GithubClient

github = GithubClient(
    client_id="YOUR_CLIENT_ID",
    client_secret="YOUR_CLIENT_SECRET",
)

# 1. Send the user's browser to this URL
authorize_url = github.get_authorize_url(scope="user:email")

# 2. The provider redirects back to your app with ?code=...
token, data = await github.get_access_token(code)

# 3. The client keeps the token, so later calls are authenticated
profile = await github.request("GET", "user")
user, raw = await github.user_info()

print(user.username, user.email)

get_access_token() returns (token, provider_data). If the provider refuses the exchange, it returns ("", data) and logs a warning; a token the client already held is left untouched.

Each client instance stores the token it received. Create one client per user/request and do not share an instance between concurrent logins. When tokens live elsewhere, pass them explicitly: await github.request("GET", "user", access_token=token).

OAuth 1.0a

from aioauth_client import TwitterClient

twitter = TwitterClient(
    consumer_key="YOUR_CONSUMER_KEY",
    consumer_secret="YOUR_CONSUMER_SECRET",
)

request_token, _ = await twitter.get_request_token()
print("Open in a browser:", twitter.get_authorize_url(request_token))

# After the user returns with an oauth_verifier / PIN code
token, data = await twitter.get_access_token(oauth_verifier)
token_secret = data["oauth_token_secret"]

twitter = TwitterClient(
    consumer_key="YOUR_CONSUMER_KEY",
    consumer_secret="YOUR_CONSUMER_SECRET",
    oauth_token=token,
    oauth_token_secret=token_secret,
)
timeline = await twitter.request("GET", "statuses/home_timeline.json")

get_access_token() stores the new oauth_token/oauth_token_secret on the instance as well, and checks the request token when one is passed.

For web apps, pass oauth_callback when requesting the token and read oauth_verifier from the callback query instead of asking for a PIN:

request_token, _ = await twitter.get_request_token(
    oauth_callback="https://app.example/oauth/twitter",
)
# The provider redirects to oauth_callback?oauth_token=...&oauth_verifier=...
token, data = await twitter.get_access_token(verifier_from_callback)

Responses and errors

request() returns parsed data, not an httpx.Response:

  • a dict for JSON responses,
  • a dict for form-encoded responses,
  • a str for anything else.

HTTP errors are not raised by default, so a 401 body comes back as data. Pass raise_for_status=True to get an OAuthError on any status >= 300; the library does this for token exchange and user_info(). Some providers report errors inside a 200 response (Slack's ok: false), and their parsers raise OAuthError themselves.

from aioauth_client import OAuthError

try:
    user, raw = await github.user_info()
except OAuthError:
    ...

Timeouts, retries and connection pooling

Without a transport, every call opens a new HTTP connection. To reuse connections and configure timeouts or retries, pass your own httpx.AsyncClient; the library never closes a client it did not create:

import httpx

transport = httpx.AsyncClient(
    timeout=10,
    transport=httpx.AsyncHTTPTransport(retries=3),
)
github = GithubClient(
    client_id="YOUR_CLIENT_ID",
    client_secret="YOUR_CLIENT_SECRET",
    transport=transport,
)

try:
    await github.request("GET", "user")
finally:
    await transport.aclose()

A per-request timeout works too: await github.request("GET", "user", timeout=30).

Provider clients

Every client below is registered in ClientRegistry.clients under the name in the first column:

from aioauth_client import ClientRegistry

client_cls = ClientRegistry.clients["github"]
Provider Client class Protocol user_info() profile
amazon AmazonClient OAuth 2.0 yes
bitbucket Bitbucket2Client OAuth 2.0 yes
discord DiscordClient OAuth 2.0 yes
eventbrite EventbriteClient OAuth 2.0 yes
facebook FacebookClient OAuth 2.0 yes
flickr Flickr OAuth 1.0a yes
foursquare FoursquareClient OAuth 2.0 yes
github GithubClient OAuth 2.0 yes
gitlab GitlabClient OAuth 2.0 yes
google GoogleClient OAuth 2.0 yes
instagram InstagramClient OAuth 2.0 yes
lichess LichessClient OAuth 2.0 yes
linkedin LinkedinClient OAuth 2.0 yes
meetup Meetup OAuth 1.0a —
microsoft MicrosoftClient OAuth 2.0 yes
mts MTSClient OAuth 2.0 yes
odnoklassniki OdnoklassnikiClient OAuth 2.0 yes
pinterest PinterestClient OAuth 2.0 yes
plurk Plurk OAuth 1.0a yes
slack SlackClient OAuth 2.0 yes
strava StravaClient OAuth 2.0 —
todoist TodoistClient OAuth 2.0 yes
trello TrelloClient OAuth 1.0a raw
tumblr TumblrClient OAuth 1.0a yes
twitter TwitterClient OAuth 1.0a yes
vimeo VimeoClient OAuth 1.0a yes
vk VKClient OAuth 2.0 yes
yandex YandexClient OAuth 2.0 yes

— in the last column means the client has no user_info_url and user_info() raises NotImplementedError. raw means the method returns the provider response but the base parser only fills User.id.

Custom provider

Subclass OAuth2Client (or OAuth1Client), set the endpoints and a user_parse generator, and the class registers itself under its name:

from aioauth_client import OAuth2Client


class MyProviderClient(OAuth2Client):
    name = "myprovider"
    base_url = "https://api.example.com"
    authorize_url = "https://example.com/oauth/authorize"
    access_token_url = "https://example.com/oauth/token"
    user_info_url = "https://api.example.com/me"

    @staticmethod
    def user_parse(data):
        yield "id", data.get("id")
        yield "email", data.get("email")
        yield "first_name", data.get("given_name")

user_parse yields (field, value) pairs consumed by User. Only fields declared in User.__slots__ are stored: id, email, first_name, last_name, username, picture, link, locale, city, country, gender, discriminator, phone, birthday. Anything else is still available in the raw provider data returned by user_info().

Development

uv sync --all-groups          # install dev and example dependencies
uv run pytest tests           # tests; also runs mypy via pytest addopts
uv run ruff check aioauth_client
uv run ruff format --check    # formatting (ruff format, not black)
uv run mypy
uv run pre-commit install     # ruff format, ruff, uv lock check, mypy, pytest

CI runs the tests on Python 3.11 through 3.15. Pushing a version tag (what make release does) re-runs the tests and publishes the build to PyPI.

To try the bundled demo app:

make example   # serves http://localhost:8080

The demo lives in example/ and needs provider credentials in example/config.py; it is not part of the library.

Contributing

Bug reports and pull requests are welcome at github.com/klen/aioauth-client. Run the checks from Development before opening a PR.

License

MIT. Copyright (c) Kirill Klenov.

Metadata

Release files for aioauth-client 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aioauth-client 1.0.0
File Size Uploaded Provenance Actions
aioauth_client-1.0.0.tar.gz 115.8 kB Without provenance Details for aioauth_client-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aioauth-client 1.0.0
File Interpreter ABI Platform Actions
aioauth_client-1.0.0-py3-none-any.whl Python 3 none any Details for aioauth_client-1.0.0-py3-none-any.whl

Total release size: 148.1 kB

Release files / aioauth_client-1.0.0.tar.gz

Download URL aioauth_client-1.0.0.tar.gz
Size 115.8 kB
Tags Source
SHA-256 checksum
How to use checksums
bcaaa3d526a3abf60cebae756b5f832a47d65666c2ba68d08b113b8191aae58e
BLAKE2b-256 checksum
How to use checksums
f2167266855f99ab9bccd1144d5f02eefcaeda0ee73e522c179eabafdf00b608
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / aioauth_client-1.0.0-py3-none-any.whl

Download URL aioauth_client-1.0.0-py3-none-any.whl
Size 32.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5ec3956919c1e0bee98eb3e281fdba5a5850ed9dc154d0b0caf6815f2e7de232
BLAKE2b-256 checksum
How to use checksums
94fe02b8d029e8b15ea2d98dbea0c72635e4331946e8d9d57f91021fc4c38f6e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for aioauth-client 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

0.0.0

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page