Skip to main content

authless

OAuth2Helper is a small wrapper around the two most common OAuth 2.0 flows:

  • Authorization Code + PKCE (for user‑level access)
  • Client Credentials (for app‑level access)

It plugs directly into requests.Session and automatically attaches the correct Authorization header, refreshes tokens, and persists them.

This guide shows only what you need to start using it.

python -m pip install authless

Quickstart

Provide a callback handler

Your application supplies auth_response_handler(login_url). It sends the user to the login URL and returns the redirect URL or path, including the full query string. The helper validates the OAuth state and exchanges the authorization code.

def local_auth_callback(login_url: str) -> str:
    print(f"Open this URL in your browser: {login_url}")
    return input("Paste the full callback URL from your browser: ").strip()

Register the same redirect URI with your OAuth provider that you pass to the helper. The snippet uses a manual callback for illustration. A web app can instead receive the redirect through its own callback route.

Create the OAuth2Helper instance

from authless import OAuth2Helper
import requests

oauth = OAuth2Helper(
    app="my-app",
    client_id="YOUR_CLIENT_ID",
    client_secret="YOUR_CLIENT_SECRET",
    token_endpoint="https://example.com/oauth2/token",
    authorization_endpoint="https://example.com/oauth2/authorize",
    redirect_uri="http://localhost:3000/callback",
    scope=("read", "write", "profile"),
    auth_response_handler=local_auth_callback,
    http_client=requests.Session(),
)

Everything else (PKCE, refresh, persistence) is handled internally.

Use it with requests

with requests.Session() as session:
    session.auth = oauth
    r = session.get("https://api.example.com/v1/me")
    print(r.json())

On the first request that requires user authentication:

  • Your callback handler directs you to the provider's login page
  • You log in
  • Tokens are stored locally
  • Future calls reuse and refresh them automatically

Strategy selection

The helper chooses between:

  • User token (Authorization Code flow)
  • App token (Client Credentials flow)

based on simple substring matching in the request URL.

Default:

user_uri_patterns = ("/user", "/me")

You can override pick_strategy() in an OAuth2Helper subclass.

Token persistence

Tokens are stored on disk using the PersistentTokens class. Each token set (user token and app token) is stored as a JSON file under:

~/.local/share/<app name>/<app | user>_token.json

The directory is created automatically on first use. The class keeps fields such as access_token, refresh_token, expires_at, and others in memory, and updates the JSON file whenever new token data is written. On startup, the helper loads the file if it exists, checks whether the token is expired, and refreshes it when needed. This makes the authentication flow run only once—subsequent runs reuse the stored tokens.

TIDAL example

The repository example demonstrates a browser-based OAuth flow with a local callback server. From a checkout, install the library with python -m pip install ., register http://127.0.0.1:3000/callback with your TIDAL app, and set TIDAL_CLIENT_ID and TIDAL_CLIENT_SECRET in your environment. Run python examples/tidal.py for a local demonstration.

Metadata

Release files for authless 0.1

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

Source distribution (sdist)

Source distribution for authless 0.1
File Size Uploaded Provenance Actions
authless-0.1.tar.gz 5.7 kB Without provenance Details for authless-0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for authless 0.1
File Interpreter ABI Platform Actions
authless-0.1-py3-none-any.whl Python 3 none any Details for authless-0.1-py3-none-any.whl

Total release size: 12.4 kB

Release files / authless-0.1.tar.gz

Download URL authless-0.1.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e177081679c007369a33fcee0a5120c1b5857c3de066633994be30378c591aeb
BLAKE2b-256 checksum
How to use checksums
43d16f62e386c5ddf69bf69a62128a2895289b7c7b39ab71cdf1155c6810bf1f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / authless-0.1-py3-none-any.whl

Download URL authless-0.1-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fe02aa383f6727c89d6361eb9748a28da3e0b8f00e2c4f73ddf7d346bf3b46ed
BLAKE2b-256 checksum
How to use checksums
93e9dc2a9d34db4c437478c232b1a88b766b78c197a3d87e23f6f251ec2731cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Security for authless 0.1

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

0.1 This release

2 release files No provenance

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