Step 4 — Token exchange

Swap the one-time code for an access token.

You have a one-time code from Step 3. Now you exchange it for an access_token, which lets you fetch the user's verified details.

This happens on your server, not in the browser.

The request

Pick the tab you prefer. curl runs as-is in a terminal. HTTP shows exactly what's sent, so you can rebuild it with any HTTP library.

curl -X POST '{hub_base_url}/oauth2/token' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'grant_type=authorization_code' \
  -d 'code=5GT81abUDETEOpVegWBZ78AejqElS-atMzTNF_7UQYQ.IfKMxHcxM5PdoDtQyNhXl5jb8uS5lxs1xc-hQHRcR8Q' \
  -d 'client_id=3f6c1a2e-8b47-4d19-9e52-7a0c4b6d1f83' \
  -d 'redirect_uri=https://acme-widgets.example.com/callback' \
  -d 'client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer' \
  -d 'client_assertion=eyJhbGciOiJSUzI1NiIsImtpZCI6ImNJbERlUjhE...'
POST {hub_base_url}/oauth2/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code
&code=5GT81abUDETEOpVegWBZ78AejqElS-atMzTNF_7UQYQ.IfKMxHcxM5PdoDtQyNhXl5jb8uS5lxs1xc-hQHRcR8Q
&client_id=3f6c1a2e-8b47-4d19-9e52-7a0c4b6d1f83
&redirect_uri=https%3A%2F%2Facme-widgets.example.com%2Fcallback
&client_assertion_type=urn%3Aietf%3Aparams%3Aoauth%3Aclient-assertion-type%3Ajwt-bearer
&client_assertion=eyJhbGciOiJSUzI1NiIsImtpZCI6ImNJbERlUjhE...

In the HTTP tab, the body is split across lines for readability. It's sent as a single line. Your HTTP library normally URL-encodes the values for you.

ParameterRequiredValue
grant_typeYesAlways authorization_code
codeYesThe code from Step 3
client_idYesYour client ID
redirect_uriYesThe same URI you used in Step 2. Must match exactly.
client_assertion_typeYesAlways urn:ietf:params:oauth:client-assertion-type:jwt-bearer
client_assertionYesA signed JWT proving who you are. See below.
code_verifierOnly with PKCESee PKCE

The client assertion

Hub has no client secret. Instead you prove your identity by signing a short JWT with your RSA private key — the same key you used in Step 2.

Header

{
  "alg": "RS256",
  "kid": "cIlDeR8D4RexrZmUdKhBxMsD2bMX4l87_5JrARh4VIU"
}

Payload

{
  "iss": "3f6c1a2e-8b47-4d19-9e52-7a0c4b6d1f83",
  "sub": "3f6c1a2e-8b47-4d19-9e52-7a0c4b6d1f83",
  "aud": "{hub_base_url}/oauth2/token",
  "exp": 1675972884,
  "jti": "35a5bae49a834eb684c8c88ef7190475"
}
ClaimRequiredTypeWhat it is
issYesStringYour client ID
subYesStringYour client ID, again
audYesStringThe token_endpoint URL from discovery
expYesNumberExpiry, as a Unix timestamp in seconds. Set it a minute or two ahead.
jtiYesStringA unique ID for this assertion. Generate a new one each time.
iatNoNumberIssued-at, as a Unix timestamp in seconds. We recommend leaving it out. See below.
🚧

exp is a number, not a string. Write "exp": 1675972884, not "exp": "1675972884". A quoted timestamp is rejected with invalid_client.

🚧

aud here is the token endpoint URL, not the issuer. This is different from Step 2, where aud is the issuer. Mixing them up is the most common cause of a rejected assertion.

🚧

Clock skew and iat

Hub checks timestamps against its own clock with no tolerance. If your server's clock runs even a second ahead, iat looks like it's from the future and the request fails with a 500 error: "The error is unrecognizable". Leave iat out (it's optional) and keep your server clock synchronised, for example with NTP. exp is still checked, so a clock running far behind can make a fresh assertion look expired.

The response

{
  "access_token": "ILcyQprq2MDuC9oxmiRDP0x1v7pvYh1UzhjuAB0nsyg.cfChPkBMaNxVWSQgSkauyRYgQrZTJ1skshsAWGr9Qds",
  "token_type": "bearer",
  "expires_in": 1800,
  "scope": "openid onlyVme_scope",
  "id_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6InB1YmxpYzo3N2U5MDEzNS00YTM1LTRkMmYt..."
}
FieldWhat it is
access_tokenUse this in Step 5
expires_inSeconds until it expires — typically 1800 (30 minutes)
token_typeAlways bearer
id_tokenA signed JWT about the verification itself. Optional to use.
❗️

Keep the access token on your server. Never send it to the browser, log it, or store it beyond the current request.

Using PKCE

If you added a code_challenge in Step 2, send the matching code_verifier you stored with the user's session:

  -d 'code_verifier=TZYQj8zTHMHRoZDa40unqDydTfOm3L2kZuPJL9DiD_s'
&code_verifier=TZYQj8zTHMHRoZDa40unqDydTfOm3L2kZuPJL9DiD_s

Hub checks it against the challenge from Step 2. If it's missing or doesn't match, the exchange fails with invalid_grant.

If you didn't send a code_challenge in Step 2, leave code_verifier out.

Common mistakes

ProblemCause
invalid_grantThe code was already used, or expired. Codes are single-use.
invalid_clientWrong aud in the assertion, missing kid, JWKS URL unreachable, or exp missing or sent as a string
500 with The error is unrecognizableiat is ahead of Hub's clock, or exp has already passed. Remove iat and check your server clock. See the clock skew note under The client assertion.
redirect_uri mismatchIt must be byte-identical to Step 2
Works once, fails on retryYou reused a jti, or replayed the code

Next

Step 5 — Retrieving claims