How to automatically set OAuth 2.0 Access Token as variable

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.

after clicking on Get New Access Token

I get the access token in the UI but i also want to be able to automatically save the tokens into variables

If i go to the console i can see the json response so i would like to find a way to access that response and save the tokens please

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

thanks

1 Like

Hey there! First, I’d like to welcome you to the Postman community and congrats on your first post! :confetti_ball:

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:

Hope this helps! :smile:

3 Likes

ok thanks for letting me know. Looking forward to the new feature being deployed!

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 tried your solution. But it doesn’t save it in Environment variables. Can you elaborate your solution? Maybe sample screenshot.

First of all, I have a global variable called “token”

Now, I have a request where I setup OAuth to get a token…

Browser opens for login…
image

Login in Browser is complete… Postman showing success, Click Proceed
image

see next post

I get a management screen showing previous tokens, Click Use token

Postman showing “Token added”… to the tab as per my understanding.

Now the tricky part. In this tab, I also have the Pre-request Script defined, to set the global variable before request…

… I Send a request, which actually don´t matter.
What matters is, that the script does write the token from the request, to my global variable.

continue in next post…

At another spot (a folder with many reuests in my case)… I use the global variable as “Bearer token”

Hope this helps :slight_smile:

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.

I have managed to created three tokens (DEV, STAGING, PROD) :

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” !