Generate access token by using refresh token

Hello team,

I am exploring on how to generate an access token using the refresh token. But, I didn’t find a way. Anyone from the community kindly help me with this. Following is auth.js file creates access token. Kindly help me with this concept. TIA

import http from 'k6/http';
import { authbaseurl } from '../../config.js';
import { check } from 'k6';

export var refresh_token;

export function accessTokenmain() {
  var requestPayload= {
    "grant_type": "client_credentials",
    "client_id": "clien_id",
    "client_secret": "client_secret", 
  };

  const response = http.post(authbaseurl,requestPayload);

  check(response, {
    'status is 200(access token is generated successfully)': r => r.status == 200,    
  });

  var authToken = response.json('access_token');
  check(authToken, { 'logged in successfully': () => authToken !== '' });

  refresh_token = response.json('refresh_token');
  console.log("Refresh Token is ============>" + refresh_token);
  
  return authToken;
};
1 Like

Hi @Niveditha

I’m not sure what the problem you’re experiencing is, especially as I’m not familiar with the system you’re testing and interacting with. It would be really tricky to give you a satisfying answer without getting more information from you.

My understanding is, you send a request to a URL, and you expect to get a JSON response back containing a access_token and a refresh_token. You seem to log them in your script, could you actually copy-paste here the (anonymized) output of your k6 test run here? Could you also log the response you get to your request by logging like the following: console.log(JSON.stringify(response, null, 4))?

That way we’d have more data points to try and help you.

Thanks! :bowing_man:

Hi Oleiade,

Thank you for the reply :grinning:. If the duration of my test exceeds an hour, the bearer token will no longer be valid and the test will fail. It would be helpful if I knew if a refresh token could be used to generate a new access token in this case. Kindly find the attached output of my k6 test run.

Hi @Niveditha,

I’m on the same boat searching for a way to refresh expiring token and share a new token among multiple VUs.

You can put your http request for login inside the setup function which will be called only once before testing and return data that contains access token and refresh token to the default function.



export function setup() {
    const response = http.post(tokenUrl, body, params);
    return response;
}

export default function (data) {
    var authToken = data.json('access_token');
    var refreshToken = data.json('refresh_token');

    // Some logic here for checking time before token expires and make http request to refresh token

    // your test code here
};

At some point, I can make another POST request to refresh the token before it expires which will return a new token. However, I cannot afford to have a dedicated token for each VU, I’m still trying to figure out how to share a new token to all VUs.

Hi @Purin,

Thanks for the reply. :grinning:

Would it be possible for you to share with me how to make another POST request to refresh the token before it expires, which will return a new token?