Redeem a ticket and refresh the tokens
The web service server redeems the ticket that the app client passed in the URL for tokens for web pages, and stores and refreshes the received tokens. For the implementation of the side that passes the ticket, see Issue a ticket and open the web page. For the overall flow, see Pass the login state to web pages.
Redeem a ticket
POST /token/v1/ticket/redeem
The web service server calls Redeem a ticket with the ticket received in the ticket query parameter of the web page URL. This API is called without the Authorization header and the X-App-Id header. The ticket determines which app and which user the tokens are for. For the call address, see Base URL.
If a request has no login state in the web service and the URL also has no ticket, prompt the user to open the web page again from the app.
Request body
| Field | Type | Required | Description |
|---|---|---|---|
ticket | string | Required | Value of the ticket query parameter in the web page URL |
serviceId | string | Required | Name of the web service that redeems the ticket. Choose one name for each web service and keep using it. For the format, see Call parameters. |
Response data
| Field | Type | Required | Description |
|---|---|---|---|
data.accessToken | string | Required | Access token for web pages |
data.refreshToken | string | Required | Refresh token for web pages |
data.expiresIn | integer | Required | Seconds remaining until the access token expires |
data.appId | string | Required | App ID of the app that obtained the ticket |
data.clientId | string | Required | Client ID used when the ticket was obtained |
For all response fields, see Response.
When the redemption succeeds, handle it as follows.
- Check that
data.appIdis the App ID of an app that the web service supports. This API redeems a ticket of any app, so if the app is not supported, discard the received tokens and tell the user that the service cannot be used. - Reloading the web page with a URL that does not include the
ticketquery parameter is recommended. This way, the already redeemed ticket is not sent again even when the user refreshes the page.
Response status
Code is the code value in the error response body. Reopen prompt means prompting the user to open the web page again from the app.
| Status | Code | Description | Web service server handling |
|---|---|---|---|
200 | - | Redeemed successfully | Store the tokens, data.appId, and data.clientId |
400 | invalid_grant_ticket · ticket_redeem_failed | When the ticket is invalid or tokens could not be issued | Do not send the request again with the same ticket. Show the reopen prompt |
400 | invalid_parameter | When the format of a request value is incorrect | Check the serviceId format. If the format is correct, show the reopen prompt |
5xx | - | When the Hive Axyl server could not process the request | Do not send the request again with the same ticket. Show the reopen prompt |
For other error codes, see Error codes by API in Redeem a ticket.
Store the tokens
The web service server stores the following values received from the redemption.
- Access token and refresh token for web pages
data.appId: Value to put in theX-App-Idheader when refreshing the tokensdata.clientId: Value to put inclientIdof the request body when refreshing the tokens
Keeping the received tokens on the web service server, without sending them to the browser, is recommended. When the tokens are kept on the web service server, they are not exposed to the browser. Keep the login state between the browser and the web service with a cookie issued by the web service.
Refresh the tokens
POST /token/v1/issue
When the access token expires, the web service server calls Issue a token with the refresh_token grant type to refresh the tokens. The access token expires after the number of seconds in data.expiresIn of the response. This API is called without the Authorization header, and data.appId from the redemption response goes in the X-App-Id header.
Request body
| Field | Type | Required | Description |
|---|---|---|---|
grantType | string | Required | refresh_token |
clientId | string | Required | data.clientId from the redemption response |
refreshToken | string | Required | The most recently received refresh token for web pages |
Response data
| Field | Type | Required | Description |
|---|---|---|---|
data.accessToken | string | Required | New access token for web pages |
data.refreshToken | string | Required | New refresh token for web pages |
data.expiresIn | integer | Required | Seconds remaining until the new access token expires |
Response status
| Status | Code | Description | Web service server handling |
|---|---|---|---|
200 | - | Refreshed successfully | Replace the stored access token and refresh token with the new values in the response |
400 | invalid_grant_refresh_token | When the refresh token is invalid | Delete the stored tokens and the login state, and show the reopen prompt |
5xx | - | When the Hive Axyl server could not process the request | Do not delete the stored tokens. Refresh again after a while |
For cases where the web page login ends and the refresh token becomes invalid, see Notes for each API in Redeem a ticket. For other error codes, see Error codes by API in Issue a token.
Redeem in the browser
A web page without a web service server may make the ticket redemption and token refresh calls from the browser. The request values, the handling after a successful redemption, and the handling for each response status are the same as on the web service server. In this case, the following handling is recommended.
- A ticket can be redeemed only within 60 seconds, so redeem it as soon as the web page opens.
- After the redemption, remove the
ticketquery parameter from the URL. This way, the already redeemed ticket is not sent again even when the page is refreshed. - Keep the received tokens in memory or in
sessionStorage, not inlocalStorage.
Related documents
- Redeem a ticket: Request and response of the Hive Axyl Server API that the web service server calls, and cases where the web page login ends
- Issue a token: Request and response of the
refresh_tokengrant type - Hive Axyl Server API error handling: Order for checking the error responses that the web service server receives