OAuth 2.0: Refresh token grant flow
The refresh token grant lets a user app obtain a new access token using a refresh token from the authorization code grant flow. Request offline_access during authorization to receive a refresh token when that scope is granted.
Org-level apps obtain new access tokens by exchanging their stored installation token. See Get an access token. Simple clients obtain new access tokens by repeating the client credentials request.
App (Client)
│
│ 1. Authenticate with client ID, secret, and refresh token
│ to request a new access token
├──────────────────────▶ Apaleo Identity API (Authorization Server)
│
│ 2. Provide an access token
│◀────────────────────── Apaleo Identity API (Authorization Server)
│
│ 3. Authenticate with the access token to request data
│ from the resource server
├──────────────────────▶ Apaleo Resource APIs (Resource Server)
│
│ 4. Provide the requested data
│◀────────────────────── Apaleo Resource APIs (Resource Server)
▼
Use expires_in to determine the access token’s lifetime. Cache the access token and reuse it until it approaches expiry.
Client requests a new token
The refresh token grant renews access without asking the user to sign in again. The refresh token must be valid, and the user who connected a fine-grained app must remain eligible. See Refresh can fail if the user is no longer eligible.
Store refresh tokens securely on your server and keep them out of browser code, URLs, and logs.
To refresh your token, using the refresh_token you already got during authorization, make a POST request to the https://identity.apaleo.com/connect/token endpoint in the authentication API, using grant_type=refresh_token.
POST https://identity.apaleo.com/connect/token
Authorization: Basic {base64(clientId:clientSecret)}
Content-Type: application/x-www-form-urlencoded
grant_type=refresh_token&refresh_token={urlEncodedRefreshToken}
| Request | Value |
|---|---|
| Method | POST |
| URL | Uses the OAuth endpoint https://identity.apaleo.com/connect/token. |
| Header | Value |
|---|---|
Authorization |
Basic authorization, using the client id and client secret of the OAuth client. |
Content-Type |
application/x-www-form-urlencoded |
Example cURL request
Set CLIENT_ID, CLIENT_SECRET, and REFRESH_TOKEN in a Bash-compatible shell. The --user option supplies HTTP Basic credentials, and --data-urlencode encodes the refresh token for the form body.
curl --request POST 'https://identity.apaleo.com/connect/token' \
--user "$CLIENT_ID:$CLIENT_SECRET" \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data 'grant_type=refresh_token' \
--data-urlencode "refresh_token=$REFRESH_TOKEN"
| Key | Description |
|---|---|
client_id |
The ID assigned to your app. Supply it as the username in HTTP Basic authentication. |
client_secret |
Your client secret. Supply it as the password in HTTP Basic authentication. |
grant_type |
Set this form field to refresh_token. |
refresh_token |
URL-encode the refresh token from the authorization code exchange as a form field. |
Refresh can fail if the user is no longer eligible
For a fine-grained user app, Apaleo checks whether the authorizing user’s role and property access cover the app’s grant each time you refresh a token. APIs that enforce delegated authorization also check the user’s access on each request. If the user loses eligibility, the refresh fails with 400 Bad Request and this error message.
{
"error": "User {userName} is no longer eligible for client {clientId} on account {accountCode}"
}
When you receive this eligibility error, take the following steps.
- Stop using the stored
refresh_tokenfor that connection, and mark the connection as disconnected in your app. - Ask the hotel to connect your app again. The connecting user’s role and property access must cover everything your app requests.
- Show the user a reconnect prompt and keep the raw error server-side. It contains a username and account code.
If refresh calls that used to succeed start failing with this response, check whether the connecting user’s role or property access changed before assuming it’s an integration bug.
For 403 Forbidden responses on API calls, check the operation’s required permission, the app’s property grant, and the user’s current access. See Handle lost access.
Approve changes to requested access
When you add scopes or permissions to a user app, ask the hotel to authorize the updated request. Refreshing a token uses the access already approved for the connection. See Approve changes to requested access.
Authorization server grants a new token
After verifying the request, the identity server sends a response with a new access token to the connected app.
The response includes a new access_token, its type, and its lifetime in seconds. An OpenID Connect response may also include an id_token.
{
"id_token": "eyJ...b9w",
"access_token": "eyJ...Vsg",
"expires_in": 3600,
"token_type": "Bearer",
"refresh_token": "E042F......5EC95",
"scope": "openid profile availability.read rates.read reservations.read identity:account-users.read offline_access"
}
The example above is for a scope client. For a fine-grained user app, scope contains identity:permissions and any requested and granted OpenID Connect scopes, such as openid profile offline_access. Determine API access from the client’s configured permissions and property grants. See Scopes and permissions.
If the response includes a refresh_token, replace the stored value with it securely before the next refresh. Otherwise, continue using the current refresh token. Cache the new access token using expires_in.
For debugging, decode test tokens locally to inspect claims such as account_code. Keep production tokens in your secure server environment.