Skip to content

OAuth token request builder

Build the token endpoint request for the authorization code, client credentials, refresh token and password grants, as curl or raw HTTP.

Runs in your browser. Nothing you paste is sent to us.

Request

The base URL, without /realms/…. Add a path if your server has one, such as /auth on old versions.

From the callback URL. Codes are single-use and short-lived.

Exactly the value sent in the authorization request.

The verifier whose challenge went into the authorization request.

A sample value is filled in. Whatever you type stays in this page.

Token request

No code_verifier. If the authorization request had a code_challenge, the server rejects the code without it.
curl command
curl 'https://auth.example.com/realms/acme/protocol/openid-connect/token' \
  -H 'Authorization: Basic d2ViLWFwcDpzYW1wbGUtc2VjcmV0LW5vdC1yZWFs' \
  --data-urlencode 'grant_type=authorization_code' \
  --data-urlencode 'code=SAMPLE_AUTHORIZATION_CODE' \
  --data-urlencode 'redirect_uri=https://app.example.com/callback'
What comes back
A JSON object with access_token, token_type, expires_in, and usually refresh_token and scope. With the openid scope it also holds an id_token. Errors come back as JSON with error and error_description.

How to use it

  1. Enter your Keycloak server URL and realm, or a custom token endpoint.
  2. Pick the grant type and fill in its fields.
  3. Choose how the client authenticates: none, Basic header, secret in the body, or a signed JWT.
  4. Copy the curl command or read the raw HTTP request.

What to know

The token endpoint takes a POST with an application/x-www-form-urlencoded body, never JSON. In Keycloak it is https://<host>/realms/<realm>/protocol/openid-connect/token.

For the code flow, send the same redirect_uri as in the authorization request and the PKCE code_verifier. Codes are single-use and short-lived.

The password grant is in the list only to debug old setups. RFC 9700 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.

For client credentials, a Keycloak client needs Client authentication on and Service accounts roles enabled.

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.

Sample request (made-up values)
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_VERIFIER

The 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.

Sample response (made-up values)
{
  "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_basic the way RFC 6749 section 2.3.1 describes, or puts it in the body for client_secret_post.
  • It takes a signed JWT you paste for private_key_jwt or client_secret_jwt, and adds the right client_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

  1. Enter your Keycloak server URL and realm, or switch to Custom URL and paste the token endpoint from your provider’s discovery document.
  2. Pick the grant type.
  3. Fill in the grant’s fields: the code, redirect URI and verifier for the code grant, or a scope for client credentials.
  4. Choose how the client authenticates, and enter the client id and secret or assertion.
  5. 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_basic and client_secret_post for 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

ErrorUsual causeFix
invalid_grantThe 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 messageThe code_verifier does not match the challenge, or is missing.Send the verifier your app stored for this sign-in.
invalid_clientWrong secret, unknown client id or wrong realm.Check the secret in the client’s credentials and the realm in the URL.
unauthorized_clientThe grant is turned off for this client.Turn on the flow in the client’s capability settings.
unsupported_grant_typeA 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.
Questions

Frequently asked questions

What content type does the OAuth token endpoint expect?

It expects `application/x-www-form-urlencoded`. RFC 6749 defines the body as form parameters. A JSON body usually fails with `invalid_request` or `unsupported_grant_type`.

Why does my token request return invalid_grant?

The code was already used or has expired, the `redirect_uri` differs from the one in the authorization request, or the PKCE verifier does not match. Start a new sign-in and exchange the new code at once with the same redirect URI and the right verifier.

Should I use client_secret_basic or client_secret_post?

Both work in Keycloak. Basic keeps the secret in a header, out of request bodies that may be logged. If you can, use a signed JWT instead, so the secret never travels.

Does client credentials return a refresh token?

It should not. RFC 6749 says a refresh token should not be included for this grant. The service simply asks for a new access token when the old one expires.

Is the password grant still allowed?

RFC 9700 says it must not be used. It exposes the password to the app and rules out multi-factor sign-in, single sign-on and passkeys. Move old apps to the authorization code flow with PKCE.

What is the Keycloak token endpoint URL?

It is `https://<host>/realms/<realm>/protocol/openid-connect/token`. The discovery document at `/realms/<realm>/.well-known/openid-configuration` lists it as `token_endpoint`.

Why do I get unauthorized_client for client credentials in Keycloak?

The client does not have Service accounts roles enabled, or it is a public client. Turn on Client authentication and Service accounts roles in the client’s settings.

Why must redirect_uri be sent again in the token request?

RFC 6749 section 4.1.3 requires it when the authorization request had one, and the values must be identical. It stops an attacker from redeeming a code that was sent to a different address. Copy the exact string your app used.

How long is an authorization code valid?

Not long. RFC 6749 recommends a maximum lifetime of 10 minutes, and servers often use less. A code can also be used only once, so exchange it as soon as the callback arrives.

Do public clients send a client_id to the token endpoint?

Yes. A public client has no secret, so it sends `client_id` in the body to say which client it is. PKCE then proves it started the sign-in.

Rather not run Keycloak yourself?

We run it for you on a cluster of your own, with backups, upgrades and written support. Starter and Premium begin with a 7-day trial.