Skip to content

How to make POST request / Get Bearer Token using OAuth2.0 using Pre-Request Script in Postman

License

Notifications You must be signed in to change notification settings

anoop0/Postman-Pre-Request-Script-Get-Bearer-Token-POST-Request

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 

Repository files navigation

Postman-Pre-Request-Script-Get-Bearer-Token-POST-Request

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.

image

For Access Token flow use following

 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/'}
]

image

In above script:

Authorization:bearer {{currentAccessToken}}

Footnote:

I refered following blog and optimzed the code for token expiry checking and Azure OAuth 2.0

Credit: https://liftcodeplay.com/2018/03/18/how-to-automatically-set-a-bearer-token-for-your-postman-requests/

About

How to make POST request / Get Bearer Token using OAuth2.0 using Pre-Request Script in Postman

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages