Token exchange

Exchange an authorization code for an access token, a refresh token and an ID token at the onefold token endpoint.

After the callback, your application exchanges the authorization code for tokens at the token endpoint. A code works once and expires 60 seconds after it is issued.

The request

POST https://onefold.eitozx.com/o/token/

The body is form-encoded (application/x-www-form-urlencoded). The token endpoint does not read JSON.

ParameterValue
grant_typeauthorization_code
codeThe code from the callback
redirect_uriThe same value you sent in the authorization request
code_verifierThe PKCE verifier, when you sent a challenge
client_idYour client ID
client_secretConfidential clients only
resourceOptional. Omit it, or repeat the value from the authorization request.
curl -X POST https://onefold.eitozx.com/o/token/ \
  -d grant_type=authorization_code \
  -d code=AUTHORIZATION_CODE \
  -d redirect_uri=https://app.example.com/callback \
  -d code_verifier=CODE_VERIFIER \
  -d client_id=YOUR_CLIENT_ID \
  -d client_secret=YOUR_CLIENT_SECRET

Client authentication

Client typeHow it authenticates
Confidentialclient_id and client_secret in the body, or as HTTP Basic credentials
Publicclient_id in the body, and no secret

A public client must not send a client_secret

If a public client sends a secret, in the body or as HTTP Basic credentials, onefold checks it and the request fails with invalid_client.

The response

{
  "access_token": "…",
  "token_type": "Bearer",
  "expires_in": 900,
  "refresh_token": "…",
  "scope": "openid identity",
  "id_token": "…"
}
FieldWhat to do with it
access_tokenSend it to the userinfo endpoint as Authorization: Bearer …. It is an opaque string; do not try to decode it.
expires_inSeconds until the access token expires
refresh_tokenStore it on your server. See Keep users signed in.
id_tokenPresent when you requested openid. Verify it before you trust it.

An access token issued for a resource is a signed JWT instead of an opaque string. See Protecting your own API.

Errors

StatusBodyMeaningWhat to do
400{"error": "invalid_grant"}The code is unknown, expired or already used, the redirect_uri differs from the authorization request, the verifier does not match the challenge, or a verifier was sent for a code requested without a challenge. A code presented again shortly after its exchange also revokes the tokens that exchange issued.Start a new sign-in
400{"error": "invalid_request"}A parameter is missing, most often code_verifierFix the request
400{"error": "invalid_target"}resource is not registered, was sent more than once, or differs from the authorization requestFix the request
401{"error": "invalid_client"}The client ID or client secret is wrongFix the credentials
429{"code": "rate_limited", "retry_after": …}Your application has used its request budget for the current minuteWait retry_after seconds, then retry
429{"code": "token_exchange_locked_out", "retry_after": …}Too many requests with a wrong client secretFix the secret. Retrying with the same one cannot succeed.

The two 429 responses use the body shape of the HTTP API, with code instead of error, and both set a Retry-After header. Only successful exchanges count toward the request budget.

From a browser

A single-page application exchanges the code from browser JavaScript, as a public client. For the browser to accept the response, add the application's origin to Allowed Origins on the OAuth2 tab.

  • An origin is a scheme, a host and an optional port, for example https://app.example.com. It has no path.
  • https is accepted for any host. http is accepted only for localhost and other loopback addresses.
  • Wildcards are not accepted.
  • An application can list up to 20 origins.

Without a matching origin the exchange succeeds on the server and the browser discards the response. Check this first when a browser exchange fails with no error body.

On this page