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:

  1. Check token expiration: Before making API requests, check if your current access token is about to expire.
  2. Prepare the refresh request: If the token is near expiration, prepare a POST request to the token endpoint.
  3. Include refresh token: In the request body, include your refresh token, which was provided along with your initial access token.
  4. 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"

Include the following parameters in the request body:

ParameterDescription
grantTypeMust be set to refresh_token
refreshTokenThe refresh token received when obtaining the access token
clientIdYour application's client ID
clientSecretYour application's client secret
  1. Handle the response: Parse the response to extract the new access token and, if provided, a new refresh token.
  2. Update stored tokens: Replace the old access token (and refresh token, if applicable) with the new ones in your application's secure storage.
  3. 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.