I have set up OAuth 2.0 Authorization with Authorization Code flow (i.e. user needs to authenticate) for a collection and i would like to automatically set the response, which includes access token, refresh token and other properties, as variables. Currently i am having to copy things manually from the UI.
Since I am using Authorization Code flow, it requires a user to sign in so it wonât be possible to just have a post request in the pre-script, which works if itâs a Client Credentials flow
Hey there! First, Iâd like to welcome you to the Postman community and congrats on your first post!
So currently we are VERY close to getting auto update/refresh of OAuth2 access tokens feature implemented.
As of now, the only way to do this is as you described, generate an access token, copy-paste it into a variable and then use that variable under the access token field.
There is also an excellent post by @allenheltondev about a workaround for token renewal that uses pre-request scripts:
Had the same question, and found a nice workaroundâŚ
In the request tab, where you use the oauth flow, the token will be saved when you click âUse tokenââŚ
Therefor in the same tab, I setup a request, and added following pre request scriptâŚ
var token = pm.request.auth.oauth2.get(âaccessTokenâ);
pm.environment.set(âaccessTokenâ, token);
Means, after clicking on âUse tokenâ, I just fire the request once, then the token will be in the variable for other tabs to consume.
I got the same scenario and your solution works for me. Thank you.
BTW, do you know how to get refresh_token from oauth2.get() ? I tried oauth2.get(ârefreshTokenâ) but it returned NULL.
Thanks
Is there an update on this functionality? Or is there a different work around? It seems like this would be a common use case to automatically populate a variable when authenticating.
Iâve tried the solution listed above but i get an error when trying to access pm.request.auth.oauth2.get(âaccessTokenâ).
If I log the pm.request.auth.oauth2 itâs undefined.
Same need here, itâs nonsense to have to create a dummy request in Postman just to manually save the token in an environment/collection variableâŚ
Iâm using it in a pre-request script, where I need to fetch data before triggering the main request.
But how to invoke them (the tokens) dynamically when I change the testing environment (I have also three env: DEV, STAGING, PROD in Postman).
It looks like that these token names are static : here is an example : I choose the testing env â01-PRODâ on the top right but the token that been used is still âTOKEN DEVâ !