I am working on set of API development which needs authorization so in headers I have to include key as Authorization
and value as Bearer <access_token>
like below:
I have another authorization api which I call and in return it gives me below data which has token value:
{
"data": {
"access_token": "eyJ0eXAiOiJKV1QiLCJub25jZSI6IkNwMTFCNXRGaWZLOZ1lOQXp5ZnZUa1hWMXhkbXJHOXBtem1xY0NRQT0iLCJhcHBfZGlzcGxheW5hbWUiOiJ1c2VyYWNjZXNzIiwiYXBwaWQiOiIyNDg4YTNiNi00MjlhLTQyZWMtYTVhZi04ZTQzZWFjNjJlMWIiLCJhcHBpZGFjciI6IjEiLCJpZHAiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC80ZjdjNzBhNy1mOGRlLTRhMzMtOGYzNS05OWE5ZDQ1NThkODUvIiwiaWR0eXAiOiJhcHAiLCJvaWQiOiJmNDk2NjUwZS1kOTgzLTRjNDItODViNC03NzYxZjljMTEzZDQiLCJyaCI6IjAuQVhjQXAzQjhUOTc0TTBxUE5abXAxRldOaGJhamlDU2FRdXhDcGEtT1EtckdMaHQzQUFBLiIsInJvbGVzIjpbIklkZW50aXR5VXNlckZsb3cuUmVhZFdyaXRlLkFsbCIsIlVzZXJTaGlmdFByZWZlcmVuY2VzLlJlYWQuQWxsIiwiVXNlckF1dGhlbnRpY2F0aW9uTWV0aG9kLlJlYWQuQWxsIiwiVXNlci5SZWFkV3JpdGUuQWxsIiwiVXNlckF1dGhlbnRpY2F0aW9uTWV0aG9kLlJlYWRXcml0ZS5BbGwiLCJEaXJlY3RvcnkuUmVhZFdyaXRlLkFsbCIsIklkZW50aXR5Umlza3lVc2VyLlJlYWRXcml0ZS5BbGwiLCJJZGVudGl0eVVzZXJGbG93LlJlYWQuQWxsIiwiVXNlci5JbnZpdGUuQWxsIiwiRGlyZWN0b3J5LlJlYWQuQWxsIiwiVXNlci5SZWFkLkFsbCIsIlVzZXJOb3RpZmljYXRpb24uUmVhZFdyaXRlLkNyZWF0ZWRCeUFwcCIsIlVzZXJTaGlmdFByZWZlcmVuY2VzLlJlYWRXcml0ZS5BbGwiLCJBcHBSb2xlQXNzaWdubWVudC5SZWFkV3JpdGUuQWxsIiwiVXNlci5FeHBvcnQuQWxsIiwiSWRlbnRpdHlSaXNreVVzZXIuUmVhZC5BbGwiLCJVc2VyLk1hbmFnZUlkZW50aXRpZXMuQWxsIl0sInN1YiI6ImY0OTY2NTBlLWQ5ODMtNGM0Mi04NWI0LTc3NjFmOWMxMTNkNCIsInRlbmFudF9yZWdpb25fc2NvcGUiOiJOQSIsInRpZCI6IjRmN2M3MGE3LWY4ZGUtNGEzMy04ZjM1LTk5YTlkNDU1OGQ4NSIsInV0aSI6IkVIYXVmb1pwazBhU2std0QyTEFlQUEiLCJ2ZXIiOiIxLjAiLCJ4bXNfdGNkdCI6MTYwODMwOTI5OH0.kEoNTY84S2sxjAlLGmfJzKYJ_20m9tOR22l3XFdrZVOR4Kv3X1ThZRvr-WckfVN0PeyQ_IIMdY7cf231MRfdMd8dTe0HdSobrBWuFel7gmdmstAWPeNLj5hIM2mQueNuyOa3PucR9qfOr0yQ-FA7I4F2UjFLN7WfU0NhP0hnI3Qg4mnnq1xc727kvZWC9KzbEszVpEnVtewLxKgWIIwRl_NsG6ghIO0utc3aJsU8f7oULVtb99gUhLM-4v5YcAk2xZebRoVnsJYv4tfg8cZJasG9POv7akzdjD8tKhjF1hOGjcowY3E0AxdiC4wYU0S7fj2Mpx4HD1gV9hPneFTgaA",
"expires_in": "3599",
"expires_on": "1618296096",
"ext_expires_in": "3599",
"not_before": "1618292196",
"resource": "https://graph.microsoft.com",
"token_type": "Bearer"
},
"msg": "",
"status": true
}
It has access_token
which I have to copy in all the other apis to authorize. This token expires every 60min so I have to copy paste the token again and again. Is there any way available through which I can define a dynamic variable in postman and assign it value of access_token
so that whenever it expires, I simply query the authorization api and value of that dynamic variable is automatically refreshed. This will help in copy pasting the token again in all the other APIs.
EDIT:
I have set the token variable in getoauthtoken api
In another api, I have now used {{token}}
but looks like its not able to find it
You should be able to use this in the Tests tab:
pm.globals.set('token', pm.response.json.data.access_token)
Once you have the variable saved, you can use {{token}}
as the header value.
If it expires, make the token request again to set the new value everywhere.
Alternatively, you could use something like this (you would need to modify the script for your context) to automatically refresh the token when it expires:
https://dannydainton.com/2018/09/10/postman-the-bearer-of-good-news/