You've already forked adk-python
mirror of
https://github.com/encounter/adk-python.git
synced 2026-03-30 10:57:20 -07:00
2367901ec5
Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 858763407
OAuth2 Client Credentials Weather Agent
This sample demonstrates OAuth2 client credentials flow with ADK's AuthenticatedFunctionTool using a practical weather assistant agent.
Overview
The OAuth2 client credentials grant type is used for server-to-server authentication where no user interaction is required. This demo shows:
- How to configure OAuth2 client credentials in ADK
- Using
AuthenticatedFunctionToolfor automatic token management - Transparent authentication in a practical weather assistant
- Testing the OAuth2 client credentials implementation
Architecture
[WeatherAssistant] -> [AuthenticatedFunctionTool] -> [OAuth2CredentialExchanger] -> [OAuth2 Server] -> [Weather API]
- WeatherAssistant calls weather tool when user asks for weather data
- AuthenticatedFunctionTool automatically handles OAuth2 flow
- OAuth2CredentialExchanger exchanges client credentials for access token
- Authenticated requests are made to weather API
Files
agent.py - WeatherAssistant Agent
Weather assistant agent that demonstrates OAuth2 client credentials flow transparently:
- OAuth2 Configuration: Client credentials setup with token URL and scopes
- Weather Tool: Single
get_weather_datatool for fetching weather information - Agent Definition: ADK LLM agent focused on providing weather information
Key Features:
- Automatic token exchange using client ID and secret
- Bearer token authentication
- Transparent OAuth2 handling (invisible to the model)
- Practical use case demonstrating machine-to-machine authentication
main.py - CLI Interface
Command-line interface for running the WeatherAssistant agent:
# Ask for weather
python contributing/samples/oauth2_client_credentials/main.py "What's the weather in Tokyo?"
Requirements:
- LLM API key (Google AI or Vertex AI)
- OAuth2 test server running
oauth2_test_server.py - Local OAuth2 Server
Mock OAuth2 server for testing the client credentials flow:
python contributing/samples/oauth2_client_credentials/oauth2_test_server.py
Features:
- OIDC discovery endpoint (
/.well-known/openid_configuration) - Client credentials token exchange (
/token) - Protected weather API (
/api/weather) - Supports both
authorization_codeandclient_credentialsgrant types - Test credentials:
client_id="test_client",client_secret="test_secret"
Endpoints:
GET /.well-known/openid_configuration- OIDC discoveryPOST /token- Token exchangeGET /api/weather- Weather API (requires Bearer token)GET /- Server info
Quick Start
- Start the OAuth2 server:
python contributing/samples/oauth2_client_credentials/oauth2_test_server.py & - Create a
.envfile in the project root with your API credentials:
# Choose Model Backend: 0 -> ML Dev, 1 -> Vertex
GOOGLE_GENAI_USE_VERTEXAI=1
# ML Dev backend config
GOOGLE_API_KEY=your_google_api_key_here
# Vertex backend config
GOOGLE_CLOUD_PROJECT=your_project_id
GOOGLE_CLOUD_LOCATION=us-central1
-
Run the agent:
# Ask for weather python contributing/samples/oauth2_client_credentials/main.py "What's the weather in Tokyo?" -
Interactive demo (use ADK commands):
# Interactive CLI adk run contributing/samples/oauth2_client_credentials # Interactive web UI adk web contributing/samples
OAuth2 Configuration
The agent uses these OAuth2 settings (configured in agent.py):
flows = OAuthFlows(
clientCredentials=OAuthFlowClientCredentials(
tokenUrl="http://localhost:8000/token",
scopes={
"read": "Read access to weather data",
"write": "Write access for data updates",
"admin": "Administrative access",
},
)
)
raw_credential = AuthCredential(
auth_type=AuthCredentialTypes.OAUTH2,
oauth2=OAuth2Auth(
client_id="test_client",
client_secret="test_secret",
),
)
Authentication Flow
- Weather Request: User asks WeatherAssistant for weather information
- Tool Invocation: Agent calls
get_weather_dataauthenticated function tool - Credential Loading: CredentialManager loads OAuth2 configuration
- Token Exchange: OAuth2CredentialExchanger uses client credentials to get access token
- Request Enhancement: AuthenticatedFunctionTool adds
Authorization: Bearer <token>header - API Call: Weather API accessed with valid token
- Response: Weather data returned to user