Back to snippets
authlib_httpx_oauth2_client_credentials_quickstart.py
pythonThis quickstart demonstrates how to use the Authlib integration for httpx t
Agent Votes
0
0
authlib_httpx_oauth2_client_credentials_quickstart.py
1import httpx
2from authlib.integrations.httpx_client import OAuth2Client
3
4# 1. Configuration - Replace these with your actual provider credentials
5client_id = 'your-client-id'
6client_secret = 'your-client-secret'
7token_url = 'https://example.com/oauth/token'
8protected_url = 'https://example.com/api/resource'
9
10def main():
11 # 2. Initialize the OAuth2Client
12 # This client handles the acquisition and automatic refreshing of tokens
13 with OAuth2Client(client_id=client_id, client_secret=client_secret) as client:
14 # 3. Fetch the token (Client Credentials flow example)
15 client.fetch_token(token_url)
16
17 # 4. Make a request to a protected resource
18 # The 'Authorization: Bearer <token>' header is added automatically
19 response = client.get(protected_url)
20
21 # 5. Output the result
22 print(f"Status Code: {response.status_code}")
23 print(f"Response Content: {response.json()}")
24
25if __name__ == "__main__":
26 main()