Service-to-service authentication using the OAuth 2.0 Client Credentials grant. No user interaction required.
Uses the Client Credentials grant. The service authenticates with its own CLIENT_ID and CLIENT_SECRET to obtain an access token.
- Python 3.10+
- uv package manager
- An AuthGate server with a configured OAuth client (with client secret)
| Variable | Required | Description |
|---|---|---|
AUTHGATE_URL |
Yes | AuthGate server URL |
CLIENT_ID |
Yes | OAuth 2.0 client identifier |
CLIENT_SECRET |
Yes | OAuth 2.0 client secret |
export AUTHGATE_URL=https://auth.example.com
export CLIENT_ID=your-client-id
export CLIENT_SECRET=your-client-secret
uv run python main.pyAlternatively, create a .env file in the python-m2m/ directory:
AUTHGATE_URL=https://auth.example.com
CLIENT_ID=your-client-id
CLIENT_SECRET=your-client-secretThen simply run:
uv run python main.pyEnvironment variables take precedence over .env values. The .env file is optional — the program works without it.
uv run automatically installs dependencies from pyproject.toml on first run.
- Auto-discovers OIDC endpoints via
DiscoveryClient - Creates an
OAuthClientwith the client secret - Creates an auto-refreshing
TokenSourcewithprofileandemailscopes and a 30-second expiry delta - Wraps the token source in a
BearerAuthhandler forhttpx - Streams an authenticated GET request to
/oauth/userinfo(limited to 1 MB) - Prints the response status and body
The token source automatically handles token acquisition and renewal — no manual refresh logic needed.
Status: 200
Body: {"sub":"service-uuid","client_id":"your-client-id",...}
- Backend services calling protected APIs
- Cron jobs and scheduled tasks
- Microservice-to-microservice communication
- CI/CD pipeline authentication