Step 4: Refresh your Access Token
To maintain continuous access to protected resources, you'll need to refresh your access token before it expires. Here are the steps to refresh your access token:
- Check token expiration: Before making API requests, check if your current access token is about to expire.
- Prepare the refresh request: If the token is near expiration, prepare a POST request to the token endpoint.
- Include refresh token: In the request body, include your refresh token, which was provided along with your initial access token.
- Send the request: Make the POST request to the token endpoint.
curl -X POST {{nxsys_base_api_url}}/nxsys/oauth/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grantType=refresh_token" \
-d "refreshToken=YOUR_REFRESH_TOKEN" \
-d "clientId=YOUR_CLIENT_ID" \
-d "clientSecret=YOUR_CLIENT_SECRET"
Nxsys Base API URL
Learn more about the Nxsys Base API URL
Learn more about the Nxsys Base API URL
Include the following parameters in the request body:
Parameter | Description |
---|---|
grantType | Must be set to refresh_token |
refreshToken | The refresh token received when obtaining the access token |
clientId | Your application's client ID |
clientSecret | Your application's client secret |
- Handle the response: Parse the response to extract the new access token and, if provided, a new refresh token.
- Update stored tokens: Replace the old access token (and refresh token, if applicable) with the new ones in your application's secure storage.
- Continue with API requests: Use the new access token for subsequent API requests.
Here's a basic example using JavaScript and the Fetch API:
async function refreshAccessToken() {
const response = await fetch('{{nxsys_base_api_url}}/nxsys/oauth/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
grantType: 'refresh_token',
refreshToken: 'YOUR_REFRESH_TOKEN',
clientId: 'YOUR_CLIENT_ID',
clientSecret: 'YOUR_CLIENT_SECRET'
})
});
const data = await response.json();
console.log(data);
return data;
}
refreshAccessToken();
Replace YOUR_REFRESH_TOKEN
, YOUR_CLIENT_ID
, and YOUR_CLIENT_SECRET
with your actual values.
This example demonstrates how to refresh an access token using a refresh token. The new access token is then used for subsequent API requests.