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.
| Parameter | Required | Value |
|---|---|---|
grant_type | Yes | Always authorization_code |
code | Yes | The code from Step 3 |
client_id | Yes | Your client ID |
redirect_uri | Yes | The same URI you used in Step 2. Must match exactly. |
client_assertion_type | Yes | Always urn:ietf:params:oauth:client-assertion-type:jwt-bearer |
client_assertion | Yes | A signed JWT proving who you are. See below. |
code_verifier | Only with PKCE | See 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"
}| Claim | Required | Type | What it is |
|---|---|---|---|
iss | Yes | String | Your client ID |
sub | Yes | String | Your client ID, again |
aud | Yes | String | The token_endpoint URL from discovery |
exp | Yes | Number | Expiry, as a Unix timestamp in seconds. Set it a minute or two ahead. |
jti | Yes | String | A unique ID for this assertion. Generate a new one each time. |
iat | No | Number | Issued-at, as a Unix timestamp in seconds. We recommend leaving it out. See below. |
expis a number, not a string. Write"exp": 1675972884, not"exp": "1675972884". A quoted timestamp is rejected withinvalid_client.
audhere is the token endpoint URL, not theissuer. This is different from Step 2, whereaudis theissuer. Mixing them up is the most common cause of a rejected assertion.
Clock skew andiatHub checks timestamps against its own clock with no tolerance. If your server's clock runs even a second ahead,
iatlooks like it's from the future and the request fails with a500error: "The error is unrecognizable". Leaveiatout (it's optional) and keep your server clock synchronised, for example with NTP.expis 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..."
}| Field | What it is |
|---|---|
access_token | Use this in Step 5 |
expires_in | Seconds until it expires — typically 1800 (30 minutes) |
token_type | Always bearer |
id_token | A 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_sHub 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
| Problem | Cause |
|---|---|
invalid_grant | The code was already used, or expired. Codes are single-use. |
invalid_client | Wrong aud in the assertion, missing kid, JWKS URL unreachable, or exp missing or sent as a string |
500 with The error is unrecognizable | iat 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 mismatch | It must be byte-identical to Step 2 |
| Works once, fails on retry | You reused a jti, or replayed the code |
Next
Updated 3 days ago