0

I configured Postman to use the authorization code flow. Problem is our tokens expiry pretty fast and I need to re-run the flow every time it expires. So I was thinking to implement a refresh_token flow in the pre-requests script (unless there is a Postman-native way to do it).

Now my question is where can I find the refresh_token? Is there any way to access it or is it 'thrown away' and only the access_token is used?

Moritz Schmitz v. Hülst
  • 3,229
  • 4
  • 36
  • 63

1 Answers1

0

Add the following code to the Collection Pre-request Script. (Edit it to your own url and body.)

// Set refresh and access tokens
const loginRequest = {
    url: pm.environment.get("mainUrl") + "/authenticate/login",  //The url that the token returns
    method: 'POST',
    header: {
        'content-type': 'application/json',
        'Accept': "*/*"
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify({    //Your body
            "username": pm.environment.get("username"),
            "password": pm.environment.get("password")
        })
    }
};

pm.sendRequest(loginRequest, function (err, res) {
    pm.environment.set("accessToken", res.json().accessToken); //The token returned in the response and the environment value to which the value will be sent
    pm.environment.set("refreshToken", res.json().refreshToken);
});

This request runs before every request.

Finally, In the "Token" field in the "Authorization" tab of the requests, call the accessToken value from the environments.

{{accessToken}}

Each time the request runs, it will refresh the token value and use this value.

Erdi
  • 51
  • 3