Back to paths

Authenticating agent tool calls with OAuth 2.0

Every tool call that reaches a protected API runs on someone's authority, either the agent's own or a user's. Tell OAuth from OpenID Connect, choose two-legged or three-legged OAuth for a tool, and handle ADK's pause for user consent.

advanced~45 min#agentic-ai#agents#security#oauth#adk
Chapter 6Secure and governLesson 3 of 5Show

Prerequisites: Agent Identity and least-privilege access, Building agents with ADK


Whose authority does this call use?

The last lesson gave the agent its own identity and narrow roles on Google Cloud. But many tools call APIs outside your project, or reach data that belongs to a user: their calendar, their mailbox, their files. For every such call, the architect decides whose authority it uses: the agent's own, or a user's who agreed to it.

That decision is made with OAuth 2.0.

OAuth authorises; OpenID Connect identifies

is an authorisation framework. It lets an application obtain limited access to an HTTP service, either on behalf of a resource owner (a user who approves it) or on its own behalf. The application gets an access token: a credential for specific protected resources, with specific scopes, that it presents on each call. The user's password is not part of it.

(OIDC) is an identity layer on top of OAuth 2.0. It adds an ID token, which carries claims about how the user signed in. Use it when the application must know who the user is.

OAuth 2.0OpenID Connect
AnswersWhat may this client access?Who is the user?
Main tokenAccess token, sent to the APIID token, read by the client
Typical agent useA tool calling an APIAn app knowing which customer is signed in

One Google Cloud exception: a private Cloud Run service or function behind IAM expects an ID token whose audience is the service URL, not an access token. ADK tools can be configured to send one.

Which token goes to an API, and which tells the app who signed in?

Two legs or three

Before the definitions, follow one tool call that needs a credential. Predict who sees Ana's password, then step through Ana's mailbox (3LO), Agent's own bucket and Ask for the password, and watch what crosses the amber credential boundary around the agent and its client app.

Simulation: oauth-consent-pause

Explain it: in Ana's mailbox flow, a code and a token cross the boundary but the password never does. Where does Ana type it, and why does the agent have to pause and wait instead of asking her for it?

Two-legged OAuth (2LO) and three-legged OAuth (3LO) are common shorthand, used in Google's documentation, for two OAuth grants.

Two-legged OAuth (2LO) uses the client credentials grant. The agent authenticates as itself, with no user involved, and gets a token for resources under its own control. Use it when the agent acts as itself: writing to its own bucket, calling an internal API on its own behalf. On Google Cloud the agent usually does this as its service account rather than with a client secret: it gets a token by signing a JWT (the JWT bearer grant) or from the metadata server, and Application Default Credentials picks the method for you. Either way no user is involved, so the shape is the same: the agent's own authority.

Three-legged OAuth (3LO) uses the authorisation code grant with user consent. The third leg is the user. The user signs in at the authorisation server and approves specific scopes; the application receives a code and exchanges it for a token that acts for that user. Because the user authenticates only with the authorisation server, their credentials are never shared with the application, or with the agent. Use it when the agent touches a user's data.

The rule: 3LO when the agent acts on a user's data with their consent; 2LO when it acts as itself. Using one powerful credential to read every user's data is the excessive agency from the threat model lesson. Three-legged OAuth keeps the agent to what each user agreed to.

An example: data agents that query as the user

Google's Conversational Analytics API, which answers questions over BigQuery and Looker data, shows the rule in practice. When an agent platform reaches it through a remote MCP server, the platform signs the user in with three-legged OAuth and passes the user's delegated access token with each request. BigQuery queries then run as that user, not as a shared service account, so the user's IAM permissions and row-level and column-level security policies apply without extra code.

Looker works differently. A query runs with the credentials configured for it: with Looker API keys (a client ID and secret), every user gets that API user's access. To apply each user's own LookML row and column permissions, send that user's access token instead.

An agent drafts replies in each employee's own mailbox. Which flow keeps it to what each employee agreed to?

Six auth models on Google Cloud

Google's Agent Identity documentation sorts every call an agent makes by two questions: whose authority it uses, and what the target accepts.

AuthorityMethodTargetUse it when
User-delegatedOAuth 2.0, 3-leggedExternal toolsThe agent acts for one user, such as on their Jira or GitHub. An auth provider handles consent and tokens.
Agent's ownAgent IdentityGoogle Cloud APIsThe agent calls Google Cloud with its own principal and grants.
Agent's ownOIDC ID tokensExternal toolsThe agent calls an external backend or another cloud that trusts Google's OIDC identity federation.
Agent's ownOAuth 2.0, 2-leggedExternal toolsMachine to machine, where the service supports OAuth. Google recommends this one for external services.
Agent's ownAPI keyExternal toolsThe service only takes a key. The auth provider stores and manages it.
Agent's ownHTTP basic authExternal toolsOnly if nothing else is offered: it sends plaintext passwords, and Google marks it not recommended.

The first two rows are what the previous lesson and this one have covered so far: 3-legged OAuth for a user's data, and the agent's own Agent Identity for Google Cloud. The OIDC row is the same identity carried outside Google as a signed ID token, which the receiver checks against public keys. The last three rows hold a third party's secret: the auth manager, below, keeps it out of your code.

Behind the table sit three identities. The user identity signs in to your app with OAuth 2.0 or OIDC. The agent identity is the agent's SPIFFE-based principal, acting on its own authority. The delegated identity is the agent acting for the user with scoped 3-legged credentials that the auth manager brokers. Audit logs record both the agent and the delegating user.

An agent posts weather alerts using a third-party weather service that only accepts an API key. No user is involved. What should hold the key?

Tool authentication in ADK

ADK describes a tool's authentication with two objects:

  • AuthScheme: how the API expects credentials, such as an OAuth 2.0 bearer token, an API key or OpenID Connect. ADK supports the same scheme types as OpenAPI 3.0.
  • AuthCredential: the initial information to start the flow, with a type such as OAUTH2, OPEN_ID_CONNECT, SERVICE_ACCOUNT or API_KEY. For OAuth that is your application's client ID, client secret and scopes.

An pairs the two for one tool or a whole toolset. ADK then tries to exchange the credential for a token before the tool calls the API. For two-legged OAuth or a service account, that exchange needs no person. For three-legged OAuth, it needs the user, so the agent pauses.

Steps
  1. The tool declares how it authenticates

    The tool or toolset carries an AuthScheme and an AuthCredential (an AuthConfig), for example OAuth 2.0 with your app's client ID, secret and the scopes it needs.

  2. The first call finds no token and requests one

    The session holds no valid credential for this user yet, so ADK pauses the run and emits an adk_request_credential event to your client app, carrying the AuthConfig and its authorisation URL.

  3. The client app returns the auth response

    Your app captures the callback URL and sends it back to ADK as the function response to adk_request_credential.

  4. ADK exchanges the code and retries the tool

    ADK exchanges the code for an access token (and possibly a refresh token), makes it available to the tool, and automatically retries the original call. The agent answers with the real result.

Two points matter for the architecture. The agent never sees the user's password: it receives a token limited to the approved scopes. And the client app owns the consent step: a chat window alone cannot complete it, so the app must handle the event and a redirect URI.

Where tokens and secrets live

Tokens and client secrets are credentials, so treat them like the agent's other secrets. ADK's documentation recommends an authentication manager service in production: one that stores keys and secrets and acquires, manages and stores OAuth tokens for you. If you manage tokens yourself, store them encrypted against an authenticated user, and keep secrets in a secrets manager, not in code, prompts or a .env file outside local development.

The auth manager: one broker for third-party credentials

Agent Identity's auth manager is Google's authentication manager for agents, and it integrates with ADK (the ADK integration was in Preview at the time of writing). You configure an auth provider for each third-party app: its type (3-legged OAuth, 2-legged OAuth or API key) and its credentials, such as an OAuth client ID and secret, a token URL or a key. The auth manager then:

  • keeps API keys, OAuth client secrets and users' tokens in a Google-managed vault;
  • runs the multi-step OAuth work itself: consent, the authorization code exchange and token refreshes, with no backend code of yours;
  • lets you see which users granted access, and revoke it.

Who may use a provider is ordinary IAM: grant the Agent Identity User role (roles/agentidentity.user) on the auth provider to the principal that uses it: the agent's identity in production, or the developer's own account while they run the agent locally. It is not granted to the end users of a 3-legged flow: they only sign in and consent at the third party's page. Auth providers live in a region, not in global, so keep the provider, the MCP server and the Agent Registry client in one region. When a tool comes from an MCP server in Agent Registry, the server's outbound binding already records which auth provider to use, so you don't build an auth scheme for it.

2-legged OAuth and API key providers use the agent's identity and need no consent: ADK retrieves the token or key from the provider and injects it into the tool's request headers. 3-legged providers need the user once. Follow Ana's first request in Ana's GitHub token through the auth manager: predict where her tokens end up, then step through the pause, her consent on GitHub's own page, and the callback. Step 9 shows her next request.

Simulation: auth-manager-flow

Explain it: in Ana's flow, the app and the agent each take part, yet neither ever holds her GitHub tokens or the OAuth client secret. What does each of them do, and what does the auth manager do in their place?

The pieces you build for a 3-legged provider:

  1. Register the callback. Creating the auth provider returns its oauthcallback URL on agentidentitycredentials.googleapis.com; register it as the redirect URI of your OAuth client at the third party.
  2. Handle the pause. When the agent returns adk_request_credential, the app sends the user to the authorization_uri and keeps the consent_nonce.
  3. Serve a continue URI. After consent the user lands on your app's continue_uri, which calls FinalizeCredentials with the nonce to complete the flow.
  4. Resume the agent with a function_response to adk_request_credential. ADK fetches the token from the provider and the tool call runs.

Put it together

Steps
  1. Follow the code to the vault

    In Ana's GitHub token through the auth manager, pick where her tokens are kept, then step to 5. Name who receives GitHub's code, who exchanges it, and where the tokens go.

  2. Compare the first and the next request

    Step on to 9. List the steps of the first request that the next one skips, and say which part of the auth manager makes that possible.

  3. Follow the code, then the token

    Step back to 4 and forward again, watching Who holds what. Say who holds the code at steps 4, 5 and 6, why it is spent after step 7, and which scope the token at step 8 carries.

  4. Compare the agent's own authority and the shortcut

    Switch to Agent's own bucket and step to the end: which columns go unused, and why is there no pause? Then try Ask for the password and step to 3. Explain why the boundary turns red, and what the agent can now do that the 3LO token could not.

Recap

  • OAuth 2.0 authorises access with tokens; OpenID Connect adds an ID token that says who the user is.
  • Two-legged (client credentials, or a service account on Google Cloud): the agent acts as itself. Three-legged (authorisation code with consent): the agent acts for a user, on that user's data.
  • In ADK, an AuthConfig pairs an AuthScheme and an AuthCredential. A three-legged tool pauses with adk_request_credential, the client app gets consent, and ADK exchanges the code and retries the call. The agent never sees a password.
  • Keep tokens and secrets in an authentication manager or a secrets manager.
  • Google's six auth models follow from two questions: whose authority, and what the target accepts. 3-legged for a user's data; Agent Identity for Google Cloud; OIDC ID tokens, 2-legged OAuth or an API key for external services on the agent's own authority; basic auth only as a last resort.
  • Agent Identity's auth manager holds third-party secrets and users' tokens in a Google-managed vault, runs consent and code exchange through an auth provider's callback, refreshes tokens, and lets ADK inject them into tool calls.

Sources

Show 12 sourcesHide sources
SourcePublisherWhat it supports
The OAuth 2.0 Authorization Framework (opens in a new tab)rfc-editor.orgIETF, RFC 6749retrieved 2026-11-17: the abstract, "obtain limited access to an HTTP service, either on behalf of a resource owner … or … on its own behalf"; section 1.3.1, "the resource owner's credentials are never shared with the client"; section 1.3.4, client credentials for resources under the client's control; section 1.4, access tokens; section 3.3, access token scope
OpenID Connect Core 1.0 incorporating errata set 2 (opens in a new tab)openid.netOpenID Foundationretrieved 2026-11-17: "a simple identity layer on top of the OAuth 2.0 protocol"; the ID Token "contains Claims about the Authentication of an End-User"
Authenticating with tools (opens in a new tab)github.comGoogle, Agent Development Kit documentationretrieved 2026-11-17: authentication manager services recommended for production; AuthScheme, AuthCredential and credential types; OAuth 2.0 authorisation code and client credentials examples; AuthConfig at toolset level; the adk_request_credential pause, redirect, auth response and automatic retry
Using OAuth 2.0 for Server to Server Applications (opens in a new tab)developers.google.comGoogleretrieved 2026-11-18: a service account creates a JWT signed with RS256 and posts it to https://oauth2.googleapis.com/token with grant_type urn:ietf:params:oauth:grant-type:jwt-bearer; client libraries abstract this, and on Google Cloud Application Default Credentials can be used instead
How Application Default Credentials works (opens in a new tab)docs.cloud.google.comGoogle Cloudretrieved 2026-11-18: the attached service account is found through the metadata server, "the preferred method for finding credentials in a production environment on Google Cloud"
Agent Identity overview (opens in a new tab)docs.cloud.google.comGoogle Cloudretrieved 2026-11-19: the auth models table, by authority, method and target, with 2-legged OAuth "recommended for machine-to-machine authentication with external services that support OAuth" and HTTP basic auth not recommended; user, agent and delegated identities
Agent Identity auth manager overview (opens in a new tab)docs.cloud.google.comGoogle Cloudretrieved 2026-11-19: auth providers "are the specific configurations used to acquire, manage, and secure API keys, OAuth client IDs, OAuth client secrets, and delegated end-user OAuth tokens"; a Google-managed vault; consent, authorization code exchange and token refreshes "without custom backend code"; visibility and revocation of end-user access
Authenticate using 3-legged OAuth with auth manager (opens in a new tab)docs.cloud.google.comGoogle Cloudretrieved 2026-11-19, rechecked 2026-12-02: the setup guide lists Agent Identity User (roles/agentidentity.user) as the role needed to use auth providers, for the account doing the setup, and does not grant it to end users; retrieveCredentials returns uri_consent_required with authorization_uri and consent_nonce; register the provider's oauthcallback redirect URI; the continue URI calls FinalizeCredentials with the nonce; resume with a function_response to adk_request_credential; tokens stored in a Google-managed credential vault; roles/agentidentity.user; an MCP server's outbound binding records the auth provider; auth providers aren't available in the global location
"Authenticate using 2-legged OAuth with auth manager" and "Authenticate using API key with auth manager" (opens in a new tab)and docs.cloud.google.com/iam/docs/auth-with-api-key-v2 (opens in a new tab)docs.cloud.google.comGoogle Cloudretrieved 2026-11-19: these providers "use the agent's identity and don't require user consent"; ADK retrieves and injects the token or key into the tool invocation headers
Agent Identity Auth Manager for ADK (opens in a new tab)github.comGoogle, Agent Development Kit documentationretrieved 2026-11-17: Google-managed lifecycle of auth credentials; Preview release
Integration patterns for data agents (opens in a new tab)docs.cloud.google.comGoogle Cloud, Conversational Analytics APIretrieved 2026-11-17: the agentic platform authenticates the user through a 3-legged OAuth 2.0 flow; queries run with the authenticated user's identity rather than a shared service account, so IAM, row-level and column-level policies apply; Looker queries use the credentials supplied for a Looker API user
Authenticate user identity and connect to a data source (opens in a new tab)docs.cloud.google.comGoogle Cloud, Conversational Analytics APIretrieved 2026-11-17: Looker API keys or an access token; an access token applies the target user's LookML row and column level permissions; with API keys all users share the same level of access

Suggested videos

Show 2 videosHide videos

Review

Flashcards1 / 7

What does OAuth 2.0 do, and what does OpenID Connect add?

Check yourself

Quick exam1 / 9 · 0 correct

An assistant agent reads each user's Google Calendar to suggest meeting times. Which OAuth flow should its calendar tool use?

Go deeper

Your notes on this lesson