What the token endpoint does
The token endpoint is where a client trades something it holds for tokens. That something is called a grant. It can be an authorization code, a refresh token, the client’s own credentials, or a few other kinds defined in later specifications.
OAuth 2.0 defines the token endpoint in RFC 6749, section 3.2. Every grant uses the same endpoint and the same shape: an HTTP POST with a form-encoded body. The grant_type parameter tells the server which kind of grant the body carries. The other parameters depend on the grant.
OpenID Connect Core 1.0 builds on the same endpoint. When the original request had the openid scope, the response also holds an ID token, a signed JWT that says who the user is.
How a token request works
Every token request follows the same rules, whatever the grant.
The request
The client sends a POST with Content-Type: application/x-www-form-urlencoded. JSON bodies are not part of the standard. A confidential client authenticates, usually with an Authorization: Basic header or with a signed JWT. A public client sends only its client_id.
POST /realms/acme/protocol/openid-connect/token HTTP/1.1
Host: auth.example.com
Content-Type: application/x-www-form-urlencoded
Authorization: Basic d2ViLWFwcDpzYW1wbGUtc2VjcmV0LW5vdC1yZWFs
grant_type=authorization_code&code=SAMPLE_AUTHORIZATION_CODE
&redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback
&code_verifier=SAMPLE_CODE_VERIFIERThe response
On success the server answers 200 with a JSON object. RFC 6749 section 5.1 lists the fields: access_token, token_type and usually expires_in, refresh_token and scope. The response must carry Cache-Control: no-store, so no proxy keeps a copy.
{
"access_token": "SAMPLE_ACCESS_TOKEN",
"token_type": "Bearer",
"expires_in": 300,
"refresh_token": "SAMPLE_REFRESH_TOKEN",
"id_token": "SAMPLE_ID_TOKEN",
"scope": "openid profile email"
}On failure the server answers 400, or 401 for a client authentication problem, with error and often error_description.
The grants this tool builds
The builder covers the four grants from RFC 6749 that people most often need to test by hand.
Authorization code
This is the grant for apps where a person signs in. After the redirect, the app sends grant_type=authorization_code, the code from the callback, the same redirect_uri it used in the authorization request, and the PKCE code_verifier. Codes are single-use and short-lived, so exchange them right away.
Client credentials
This is the grant for a service that acts as itself, with no user. It sends grant_type=client_credentials and authenticates with its own credentials. RFC 6749 section 4.4.3 says no refresh token should come back; the service asks again when the token expires.
Refresh token
This grant swaps a refresh token for a new access token. It is covered in depth by the refresh request builder.
Password, for debugging only
The resource owner password credentials grant sends the user’s username and password to the token endpoint. RFC 9700, the OAuth 2.0 security best current practice, says it must not be used. The app sees the password, and the flow cannot do multi-factor sign-in, single sign-on or passkeys. The builder includes it only so you can reproduce and debug an old setup while you move it to the code flow.
What this tool does and does not do
The builder turns your inputs into a request you can run. It shows a curl command and the raw HTTP request, side by side with the choices that shaped them.
- It builds the Keycloak token endpoint from a server URL and realm, or takes any custom URL.
- It encodes the client secret for
client_secret_basicthe way RFC 6749 section 2.3.1 describes, or puts it in the body forclient_secret_post. - It takes a signed JWT you paste for
private_key_jwtorclient_secret_jwt, and adds the rightclient_assertion_type. - It warns when the code grant has no
code_verifier, and when the password grant is chosen.
It does not send the request. Nothing leaves your browser. It does not sign client assertions, and it cannot know whether your code is still valid. Run the curl command yourself to see the server’s answer.
How to use the builder
- Enter your Keycloak server URL and realm, or switch to Custom URL and paste the token endpoint from your provider’s discovery document.
- Pick the grant type.
- Fill in the grant’s fields: the code, redirect URI and verifier for the code grant, or a scope for client credentials.
- Choose how the client authenticates, and enter the client id and secret or assertion.
- Copy the curl command and run it from a terminal, or read the HTTP view to compare with what your app sends.
Sample values are filled in so the output is never empty. Replace them with your own; they stay in the page.
Where it helps
Web apps with a back end
A server-side app is a confidential client. It exchanges the code with its secret and the PKCE verifier. When a library hides the request, building it by hand shows exactly which parameter the server rejects.
Single-page and mobile apps
Browser and mobile apps are public clients. They send client_id instead of a secret, and PKCE is what protects the code. Pick None in the client authentication list to see that shape.
Service-to-service calls
A background job or API that calls another API uses client credentials. Testing the request by hand confirms the client is set up before you write code.
How Keycloak handles token requests
In Keycloak the token endpoint is https://<host>/realms/<realm>/protocol/openid-connect/token. Each realm has its own.
- The code grant needs Standard flow turned on for the client.
- Client credentials needs Client authentication on and Service accounts roles enabled. The token then describes the client’s service account, not a person.
- The password grant needs Direct access grants on. Leave it off for new clients.
- Keycloak accepts both
client_secret_basicandclient_secret_postfor a client that uses a client secret.
If you would rather not run Keycloak yourself, our plans cover a managed, upstream Keycloak on a cluster of your own.
Common errors and how to fix them
| Error | Usual cause | Fix |
|---|---|---|
invalid_grant | The code was used twice or expired, or the redirect URI differs. | Start a new sign-in. Send the exact redirect_uri from the authorization request. |
invalid_grant with a PKCE message | The code_verifier does not match the challenge, or is missing. | Send the verifier your app stored for this sign-in. |
invalid_client | Wrong secret, unknown client id or wrong realm. | Check the secret in the client’s credentials and the realm in the URL. |
unauthorized_client | The grant is turned off for this client. | Turn on the flow in the client’s capability settings. |
unsupported_grant_type | A typo in grant_type, or a JSON body. | Send a form-encoded body with the exact grant name. |
For any other code, the OAuth error lookup explains what it means and what usually causes it.
Security notes
RFC 9700 asks for PKCE on every authorization code flow, confidential clients included. It also says the password grant must not be used and the implicit grant should not be used.
Keep client secrets on servers. A secret in a browser or mobile app is not a secret. For services, a signed JWT (private_key_jwt) or mutual TLS avoids sending a shared secret at all.
Treat curl commands with real secrets or codes like the secrets themselves. They end up in shell history. Use sample values when you share a command.
Best practices
- Exchange codes at once, from your back end when you have one.
- Use one redirect URI per environment and send it byte for byte.
- Cache client credentials tokens until shortly before they expire instead of asking for each call.
- Read
error_description. Keycloak’s messages often name the cause. - Log the error code, never the token or the secret.