0

A fresher to postman, currently working on API project where I need to delivery to the API and Token the client to integrate with them system, good is I successfully configure the Authorization as OAuth Type as Password Credentials and receiving perfect response as 200.

The issue/confusion is Token is getting expire every hour, I need to Get new Access Token every time.

So, the question is, is it anyway I can overcome this issue?

  • that no need to get new/refresh token.
  • can provide the one fix token to client.

1 Answers1

0

You can do it like here. You can get the token in the pre-request field of the collection or request.

https://stackoverflow.com/a/73911458/10126763

EDIT We can adapt it like this:

Take this and paste it in the "pre-request" field of the collection or the request you will use directly. Create an "environment" value named "accessToken". When each request is run, this method will run first and send the token value to the value in the environment.

// Set refresh and access tokens
const loginRequest = {
   url:  "exampleurl.com/etc/etc", //YOUR URL
    method: 'GET',
    header: {
        'content-type': 'application/json',
        'Accept': "*/*"
    }  //Since you will be using GET, I deleted the body. If you are sending value you can get the body field from the other example in the link.
};

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
});
Erdi
  • 51
  • 3
  • Hi @Erdi, thank you for your reply. First time using code in Postman. That code is for Post method I guess and I am using get at moment. So do I just need to change the POST to GET is it? Received error `There was an error in evaluating the Pre-request Script:Error: Unexpected token u in JSON at position 0` – user20042770 Oct 06 '22 at 07:40
  • Also, does 'mainUrl' variable refers entire API URL ? – user20042770 Oct 06 '22 at 07:42
  • Yes, you need to change it to "get". I'm editing the comment on this page, you can take it and use it. – Erdi Oct 06 '22 at 09:56
  • Hi @Erdi thank you very much for your help. Sorry I made you confuse , you link code works with my situation. So if you remove this edited code I would like to accept your answer. Or I can update my used code for community. Many thanks. – user20042770 Oct 06 '22 at 11:48
  • Please help me if you have any solutions for this question as well or suggestion https://stackoverflow.com/questions/73961774/how-customize-api-url-in-postman – user20042770 Oct 06 '22 at 11:53