This repository contains code that was used for illustrating the article titled "OAuth Authentication for Python CLI Tools: A Complete Guide"
Pick your provider and register an OAuth application:
GitHub
- Visit GitHub Developer Settings
- Create a new OAuth App
- Set the Authorization callback URL to
http://localhost:8080/callback - Note your
Client IDandClient Secret
- Visit Google Cloud Console
- Create an OAuth 2.0 Client ID (application type: "Web application")
- Add
http://localhost:8080/callbackto Authorized redirect URIs - Note your
Client IDandClient Secret
cd <your repo folder name>
uv venv && source .venv/bin/activate
uv syncSet provider credentials via environment variables:
# GitHub (default)
export GITHUB_CLIENT_ID="your_client_id"
export GITHUB_CLIENT_SECRET="your_client_secret"
python oauth_cli.py
# Google
export GOOGLE_CLIENT_ID="your_client_id"
export GOOGLE_CLIENT_SECRET="your_client_secret"
python oauth_cli.py --provider googleEither or both can be supplied as arguments, overriding the corresponding env var:
python oauth_cli.py --client-id 9876 --client-secret pqrs| Flag | Default | Description |
|---|---|---|
--provider |
github |
OAuth provider (github, google) |
--client-id |
— | Override env-var-based client ID |
--client-secret |
— | Override env-var-based client secret |