How to make POST request / Get Bearer Token using OAuth2.0 using Pre-Request Script in Postman
There are lots of usecases where we need to get Bearer token before making any request from Postman. To simplfy the process of getting bearer token from another request and then copy the token into differnt request, we can use Pre-request scripts.
The given script can be used to for any POST request or RESTful call in general. Script sets the access token in global varaiable to used in header.
urlencoded:[
{key:'grant_type', value:'client_credentials'},
{key:'client_id', value:'<CLIENT_ID>'},
{key:'client_secret', value:'<Secret>'},
{key:'resource', value:'https://graph.microsoft.com/'}
]
Authorization:bearer {{currentAccessToken}}
I refered following blog and optimzed the code for token expiry checking and Azure OAuth 2.0