Skip to main content
Security Deep DiveD7 Security

OAuth 2.0 for AI Agents: The Client Credentials Flow Every API Needs

Most OAuth flows assume a human is sitting at a browser, clicking “Allow.” AI agents are not humans. They cannot click consent screens, follow redirects, or solve CAPTCHAs. There is exactly one OAuth flow built for machines: client_credentials. It is the gold standard for agent authentication, and most APIs do not support it.

AH
AgentHermes Research
April 15, 202613 min read

The Authentication Problem Agents Face

OAuth 2.0 is the internet’s authentication standard. Every major API uses it. But OAuth was designed in 2012 with one assumption: a human is in the loop. The most common flow — Authorization Code — works like this: redirect the user to a login page, they enter credentials, they click “Allow,” they get redirected back with a code, the code gets exchanged for a token.

AI agents cannot do any of that. They do not have browsers. They do not have fingers to click buttons. They do not have eyes to read consent screens. When an agent encounters an OAuth flow that requires browser interaction, it hits a wall. The integration fails. The agent moves on to a competitor that offers programmatic authentication.

This is not a theoretical problem. In our scan of 500 businesses, 73% of APIs that offer OAuth only support browser-based flows. That means nearly three out of four OAuth implementations are invisible to AI agents — even though the API behind them works perfectly.

73%
OAuth APIs with browser-only flows
0.12
D7 Security weight in ARS
4
OAuth grant types defined
1
that works for agents

The Four OAuth Flows: Only One Works for Agents

OAuth 2.0 defines four grant types. Three require a browser. One does not. Here is the breakdown and why it matters for agent readiness.

Authorization Code

Not for agents

User clicks login, gets redirected to a consent screen, approves access, gets a code, then exchanges it for a token. The standard web login.

Agent verdict: Requires a browser, a user clicking buttons, and a redirect URI. Agents do not have browsers.

Authorization Code + PKCE

Not for agents

Same as Authorization Code but with a proof key for code exchange. Designed for public clients like mobile apps where you cannot store a secret safely.

Agent verdict: Still requires a browser and user interaction. The PKCE extension solves a different problem.

Implicit (Deprecated)

Not for agents

Skips the code exchange and returns the token directly in the URL fragment. Deprecated in OAuth 2.1 for security reasons.

Agent verdict: Deprecated, insecure, and still requires a browser redirect. Do not use.

Client Credentials

Agent-friendly

POST to /oauth/token with client_id + client_secret, get an access_token back. No browser. No redirect. No consent screen. Pure machine-to-machine.

Agent verdict: This is the flow agents need. One HTTP request, one response, one token. Done.

How Client Credentials Works: The 3-Step Agent Auth Flow

The client_credentials flow is the simplest OAuth flow. No browser, no redirect, no human. Three steps, three HTTP interactions, done.

1

Agent sends credentials to the token endpoint

POST /oauth/token with grant_type=client_credentials, client_id, and client_secret. Can be in the body or as Basic auth header. Content-Type: application/x-www-form-urlencoded.

POST /oauth/token
client_id=agent_abc123&client_secret=sk_live_xxx&grant_type=client_credentials
2

Server returns an access token

The authorization server validates the credentials and returns a JSON response with access_token, token_type, expires_in, and optionally a scope. No redirect, no code exchange.

{ "access_token": "eyJhbGciOiJSUzI1...", "token_type": "Bearer", "expires_in": 3600 }
3

Agent uses the token on every API call

The agent includes the token in the Authorization header of every subsequent request. When it expires, the agent repeats step 1 to get a fresh token.

GET /v1/products
Authorization: Bearer eyJhbGciOiJSUzI1...

That is it. The entire authentication handshake is one POST request. Compare that to Authorization Code flow, which requires: a redirect URL, a browser session, user consent, a code exchange, and state parameter validation. Client credentials removes all five of those steps because there is no user — just two machines talking.

Who Gets It Right: Top Agent Readiness Scorers on D7 Security

The highest-scoring businesses in our 500-business scan all support programmatic authentication that agents can use without a browser.

Stripe

68/100

Bearer + restricted keys + OAuth Connect with client_credentials

GitHub

67/100

OAuth apps + GitHub Apps (JWT) + fine-grained PATs

Slack

68/100

Bot tokens via OAuth 2.0 client_credentials flow

Resend

75/100

API keys (Bearer) + scoped permissions per key

Notice the pattern: every top scorer supports at least one form of non-interactive authentication. Stripe offers restricted API keys and OAuth Connect with client_credentials. GitHub supports GitHub Apps using JWT-based authentication. Slack provides bot tokens via OAuth client_credentials. Resend uses scoped API keys with Bearer headers. None of them require a browser to get started.

Five Anti-Patterns That Block Agents From Authenticating

These patterns are common across the 500 businesses we scanned. Every one of them prevents AI agents from connecting, even when the underlying API is excellent.

Browser-only OAuth for API access

Requiring users to click through a consent screen to get an API token. If the only path to a token is a browser, agents cannot authenticate.

Fix: Add a client_credentials grant alongside your authorization code flow. Both can coexist.

CAPTCHA before token issuance

Putting a CAPTCHA on the OAuth endpoint or API key creation page. Agents cannot solve CAPTCHAs.

Fix: Rate-limit the token endpoint instead. Use IP-based throttling and monitor for abuse programmatically.

Session cookies instead of Bearer tokens

APIs that only accept session cookies set by a browser login flow. No Authorization header support at all.

Fix: Accept Bearer tokens in the Authorization header. Session cookies are for browsers, not agents.

Manual API key approval

Requiring a human to review and approve API key requests. Creates hours or days of latency before an agent can connect.

Fix: Offer instant self-service keys with rate limits. Review high-volume usage after the fact, not before.

Token rotation without programmatic refresh

Expiring tokens without providing a refresh_token or a re-issuance endpoint. The agent loses access and cannot recover.

Fix: If tokens expire, always include a refresh_token or let client_credentials re-issue instantly.

What AgentHermes Checks in D7 Security

D7 Security carries a 0.12 weight in the Agent Readiness Score. Here are the specific auth signals AgentHermes scans for, ranked by impact.

Signal
What We Check
Score Impact
401 + JSON body
Unauthenticated request returns 401 with structured error
87% of 200 score
Bearer token support
API accepts Authorization: Bearer header
High positive
OAuth discovery
/.well-known/openid-configuration or /oauth/token endpoint
High positive
Self-service keys
API keys available without human approval
Medium positive
Token expiration
Tokens expire and can be refreshed programmatically
Medium positive
Scope restriction
Tokens can be scoped to specific permissions
Low positive

Key finding: A 401 response with a structured JSON error body scores 87% as much as a successful 200 response. Why? Because it tells the agent exactly what went wrong and what authentication it needs. A 403 Forbidden HTML page tells the agent nothing. The structured 401 is the single most impactful D7 signal.

The 30-Minute Implementation

If you already have an API, adding client_credentials support takes 30 minutes. If you are building an MCP server, add it at the same time. Here is the minimal checklist.

Create a POST /oauth/token endpoint that accepts grant_type=client_credentials

Validate client_id + client_secret from the request body or Basic auth header

Return { access_token, token_type: "Bearer", expires_in } as JSON

Accept Authorization: Bearer {token} on all protected API endpoints

Return 401 + JSON { error: "unauthorized", message: "..." } on invalid/missing tokens

Add /.well-known/openid-configuration pointing to your token endpoint (optional but recommended)

Document the flow in your OpenAPI spec under securitySchemes

That is seven items. Most can reuse your existing auth infrastructure. If you already validate API keys, you already have step 5. If you already have an OAuth server for browser login, you only need to add the client_credentials grant type — the token validation middleware is the same.

Frequently Asked Questions

What is the client_credentials grant?

The client_credentials grant is an OAuth 2.0 flow designed for machine-to-machine communication. The client sends its client_id and client_secret to the authorization server and receives an access_token. No user interaction, no browser, no redirect. It is the only standard OAuth flow that works for AI agents out of the box.

Do agents need OAuth at all? Why not just API keys?

API keys work and are better than no auth. But OAuth client_credentials provides scoped access, token expiration, and rotation without changing the key itself. For agents managing multiple services, OAuth is more secure because a compromised token expires automatically. AgentHermes gives full D7 Security credit for either Bearer tokens or OAuth.

How does AgentHermes check for OAuth support?

AgentHermes checks D7 Security (0.12 weight) for multiple auth signals: Bearer token acceptance on API endpoints, 401 + structured JSON on unauthenticated requests, OAuth discovery endpoints (/.well-known/openid-configuration or /oauth/token), and self-service key creation. Client_credentials support is the strongest positive signal.

Can I support both browser OAuth and client_credentials?

Yes, and you should. Authorization Code flow serves your human users logging into your dashboard. Client Credentials flow serves AI agents and automation. Both use the same OAuth infrastructure and can share the same authorization server. The only difference is the grant_type parameter in the token request.


How does your API score on D7 Security?

Run a free Agent Readiness Scan and see your D7 Security score. Find out if agents can authenticate with your API — or if they are hitting a wall.


Share this article: