Skip to content

OAuth device flow request builder

Build the device authorization request and the polling request for the OAuth device flow (RFC 8628), for TVs, consoles and other devices.

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

Settings

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

Separated by spaces. Add offline_access for a token that outlives the session.

From the device authorization response. A placeholder is filled in.

In Keycloak, turn on OAuth 2.0 Device Authorization Grant in the client’s capability settings. Devices usually run as public clients, since they cannot keep a secret.

The three steps

1. Ask for a device code

curl command
curl 'https://auth.example.com/realms/acme/protocol/openid-connect/auth/device' \
  --data-urlencode 'scope=openid profile' \
  --data-urlencode 'client_id=web-app'
Sample response (made-up values)
{
  "device_code": "SAMPLE_DEVICE_CODE",
  "user_code": "WDJB-MJHT",
  "verification_uri": "https://auth.example.com/realms/acme/device",
  "verification_uri_complete": "https://auth.example.com/realms/acme/device?user_code=WDJB-MJHT",
  "expires_in": 600,
  "interval": 5
}

2. Show the user where to go

Show verification_uri and user_code, or a QR code of verification_uri_complete. The user opens it on a phone or laptop, signs in and confirms the code.

3. Poll the token endpoint

curl command
curl 'https://auth.example.com/realms/acme/protocol/openid-connect/token' \
  --data-urlencode 'grant_type=urn:ietf:params:oauth:grant-type:device_code' \
  --data-urlencode 'device_code=SAMPLE_DEVICE_CODE' \
  --data-urlencode 'client_id=web-app'
authorization_pending
The user has not finished. Wait interval seconds, then ask again.
slow_down
Add 5 seconds to the interval, then keep polling.
access_denied
The user declined. Stop.
expired_token
The device code expired. Start again at step 1.
200 OK
The tokens: access_token, and usually refresh_token and id_token.

How to use it

  1. Enter your Keycloak server URL and realm, or custom endpoints.
  2. Enter the client id and scopes; devices are usually public clients.
  3. Copy the first request to get a device_code and user_code.
  4. Show the user the code and URL, then poll with the second request.

What to know

The device flow is for devices without a good browser or keyboard. The device shows a short code, the user signs in on a phone or laptop, and the device polls the token endpoint until it gets tokens.

While waiting, the server answers authorization_pending. On slow_down, add 5 seconds to the interval. Stop on access_denied or expired_token.

In Keycloak, turn on OAuth 2.0 Device Authorization Grant for the client. The device endpoint is https://<host>/realms/<realm>/protocol/openid-connect/auth/device.

Why the device flow exists

The usual OAuth sign-in sends the user to a web page and back to the app through a redirect. That works when the app runs in a browser or can open one. It does not work well on a smart TV, a game console, a printer or a terminal on a remote server. These devices either have no browser or have one that is painful to type into.

The OAuth 2.0 Device Authorization Grant, RFC 8628, solves this by splitting the work across two devices. The device asks for a short code and shows it. The user opens a URL on a phone or laptop, signs in there and enters the code. Meanwhile the device polls the token endpoint until the server says the user is done.

Because the user signs in on their own device, they can use everything the server offers: passwords, one-time codes, passkeys and identity providers. The limited device never sees the password.

How the flow works, step by step

1. Device authorization request

The device sends a POST to the device authorization endpoint with its client_id and the scopes it wants.

Sample request (made-up values)
POST /realms/acme/protocol/openid-connect/auth/device HTTP/1.1
Host: auth.example.com
Content-Type: application/x-www-form-urlencoded

client_id=tv-app&scope=openid%20profile
Sample response (made-up values)
{
  "device_code": "SAMPLE_DEVICE_CODE",
  "user_code": "WDJB-MJHT",
  "verification_uri": "https://auth.example.com/realms/acme/device",
  "verification_uri_complete": "https://auth.example.com/realms/acme/device?user_code=WDJB-MJHT",
  "expires_in": 600,
  "interval": 5
}

2. User interaction

The device shows verification_uri and user_code. It can also show a QR code of verification_uri_complete, which carries the code so the user does not have to type it. The user signs in on the second device and confirms.

3. Polling the token endpoint

While the user works, the device polls the token endpoint with grant_type=urn:ietf:params:oauth:grant-type:device_code and the device_code. It waits interval seconds between attempts, or 5 seconds if the server gave none.

Sample polling 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

grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Adevice_code
&device_code=SAMPLE_DEVICE_CODE&client_id=tv-app

When the user finishes, the poll returns a normal token response.

Polling responses and what to do

RFC 8628 section 3.5 defines the answers a device gets while it polls. Handle each one explicitly.

ResponseMeaningWhat the device does
authorization_pendingThe user has not finished.Wait interval seconds and poll again.
slow_downStill pending, and polling too fast.Add 5 seconds to the interval, then keep polling.
access_deniedThe user declined.Stop and tell the user.
expired_tokenThe device code expired.Start again with a new device authorization request.
200 with tokensThe user approved.Store the tokens and stop polling.

What this tool does and does not do

The builder writes both requests of the flow, the device authorization request and the polling request, as curl commands and raw HTTP.

  • It builds Keycloak’s device and token endpoints from a server URL and realm, or takes two custom URLs.
  • It defaults to a public client, which suits most devices, and also supports secrets and signed JWTs.
  • It shows a sample device authorization response and a table of polling answers.

It does not run the flow, poll for you or keep a timer. Paste the device_code from your real response into the polling field, then run the command yourself at the interval the server gave.

How to use the builder

  1. Enter your Keycloak server URL and realm, or switch to Custom URL and paste both endpoints.
  2. Enter the scopes. Add offline_access only if the device must keep working after the session ends.
  3. Set the client id and client authentication.
  4. Run the first request and note device_code, user_code and interval.
  5. Open the verification URL in a browser, sign in and enter the user code.
  6. Paste the device code into the builder and run the polling request until tokens come back.

Where the device flow fits

TVs, consoles and kiosks

This is the classic case: a big screen, a remote control and no keyboard. Show a short code and a QR code, and let the user finish on a phone.

Command-line tools

A CLI that may run over SSH, inside a container or on a headless server cannot rely on opening a browser. The device flow lets the user approve from any machine. On a desktop with a browser, the code flow with a loopback redirect is usually smoother, because the user does not have to copy a code.

Devices without a screen keyboard

Smart speakers, printers and other appliances can show a code on a small display or read it aloud, then poll. The only requirements are an internet connection and a way to show the user a short code.

Building the polling loop

Most bugs in device flow clients sit in the polling loop. A correct loop is short, but each branch matters.

  1. Read interval from the device authorization response, or use 5 seconds.
  2. Compute a deadline from expires_in and stop when it passes.
  3. Wait the interval, then send the polling request.
  4. On authorization_pending, loop again with the same interval.
  5. On slow_down, add 5 seconds to the interval for all later polls.
  6. On access_denied or expired_token, stop and tell the user what happened.
  7. On a 200 response, store the tokens and leave the loop.

Treat network errors and 5xx answers as temporary: wait and try again, but stay within the deadline. RFC 8628 says a client must slow down after a connection timeout and recommends doubling the interval each time. Never poll faster than the server asks.

Run the loop in the background so the device can still show the code, a countdown and a cancel button.

How Keycloak handles the device flow

Keycloak supports RFC 8628. The device authorization endpoint is https://<host>/realms/<realm>/protocol/openid-connect/auth/device, and polling goes to the usual token endpoint.

  • Turn on OAuth 2.0 Device Authorization Grant in the client’s capability settings.
  • Devices usually run as public clients, with Client authentication off, because a secret on a device can be extracted.
  • The discovery document lists the endpoint as device_authorization_endpoint and the grant in grant_types_supported.

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

  • unauthorized_client on the first request: the grant is not enabled for the client. Turn on OAuth 2.0 Device Authorization Grant.
  • invalid_client: the client id is wrong, or the client is confidential and no credentials were sent.
  • slow_down again and again: the device does not add 5 seconds after each one. Keep the larger interval for the rest of the flow.
  • expired_token: the user took longer than expires_in. Start over and show a fresh code.
  • invalid_grant while polling: the device code was already used or belongs to another client.

The OAuth error lookup explains any other code you see.

Security notes

The device flow has one known weakness: an attacker can start a flow, then trick someone into entering the attacker’s user code on a real sign-in page. RFC 8628 section 5.4 discusses this remote phishing risk. Show the user which device and app they are approving, and keep codes short-lived.

User codes are short so people can type them, which also makes them guessable. RFC 8628 asks servers to limit attempts at the verification page. Device codes themselves must stay secret, since anyone holding one can poll for the tokens.

As RFC 9700 advises for all flows, ask only for the scopes the device needs.

Best practices

  • Show both the URL and the code in large type, and add a QR code of verification_uri_complete when the screen allows.
  • Respect interval and slow_down exactly.
  • Stop polling at expires_in instead of waiting for expired_token.
  • Store refresh tokens in the device’s protected storage.
  • Let the user cancel and start over from the device.
Questions

Frequently asked questions

What is the OAuth device authorization grant?

It is the flow from RFC 8628 for devices without a good browser or keyboard. The device shows a short code, the user approves on a phone or laptop, and the device polls the token endpoint until it gets tokens.

How often should the device poll the token endpoint?

Every `interval` seconds from the device authorization response, or every 5 seconds if none is given. After a `slow_down` answer, add 5 seconds to the interval and keep the larger value.

What does authorization_pending mean?

The user has not finished approving yet. It is the normal answer while the device waits. Poll again after the interval.

How do I enable the device flow in Keycloak?

Turn on OAuth 2.0 Device Authorization Grant in the client’s capability settings. The device endpoint is `/realms/<realm>/protocol/openid-connect/auth/device`.

Should a CLI use the device flow?

Use it when the CLI may run where no browser is available, such as over SSH. On a desktop, the authorization code flow with PKCE and a loopback redirect avoids copying a code.

Does the device flow need a client secret?

Usually not. Devices cannot keep a secret safe, so they run as public clients. A confidential client can still use the flow if it has a safe place for its credentials.

What is verification_uri_complete?

It is the verification URL with the user code already in it. Show it as a QR code so the user can scan it and skip typing the code.

How long is a device code valid?

As long as `expires_in` in the device authorization response says. When it runs out, polling returns `expired_token` and the device must start again with a new code.

Is the device flow secure?

It is safe when used as designed, but it can be abused for phishing by tricking users into entering an attacker’s code. Show clear context on the approval page and keep codes short-lived.

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.