# Automaticaly re-auth with OAuth2

**URL:** <https://community.postman.com/t/automaticaly-re-auth-with-oauth2/9125>\
**Category:** Help Hub\
**Tags:** oauth2\
**Created:** [November 20, 2019, 9:51am UTC](https://community.postman.com/t/automaticaly-re-auth-with-oauth2/9125 "2019-11-20T09:51:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![simfre](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@simfre](https://community.postman.com/u/simfre)\
**Post date:** [November 20, 2019, 9:51am UTC](https://community.postman.com/t/automaticaly-re-auth-with-oauth2/9125/1 "2019-11-20T09:51:58Z")

</div>

How can I set Postman to automatically get a new OAuth2 token when the previous one expires? Would be nice to either just have a quick button och even better for it to check when sending the request.

---

<div class="post-metadata">

**Author:** ![odanylewycz](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/odanylewycz/32/3240_2.png) [@odanylewycz](https://community.postman.com/u/odanylewycz)\
**Post date:** [November 20, 2019, 3:10pm UTC](https://community.postman.com/t/automaticaly-re-auth-with-oauth2/9125/2 "2019-11-20T15:10:28Z")

</div>

Hi @simfre,

Welcome to the community! 👏

This should be fairly straight forward, but before I layout some code, I am going to make some assumptions.

If you get an expiration time with each issuance of an access token (in general OAuth2 implementation, you should), you can store that token and expiration time in environment variables.

Then each time you make a request, you compare the current time to the time you got from the token by pulling down that environment variable. If the current time is greater than the time from the token, we know it has expired. Then you use `pm.sendRequest` method to get another access token, store the token and time as environment variables again, for use in your main request. Else, the current time is less than the token time, it has not expired, and you can skip getting the new token.

Thats the process that I would lay out, I hope it makes sense.

Without further detail on how your OAuth2 request looks, I can’t add much additionally here.

Here is some general pseudo code that should explain it.

`var token_time = pm.environment.get("token_time");`  
`var access_token = pm.environment.get("access_token");`

`// Check if Token expired`  
`if ( Date.now() > token_time ) {`

`pm.sendRequest(...)`

`// Parse response to get new token and token time, set as env variables`

`pm.environment.set("token_time", new_token_time);`  
`pm.environment.set("access_token", new_access_token);`

`};`

Hope this helps!

Regards,  
Orest

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [November 20, 2019, 6:25pm UTC](https://community.postman.com/t/automaticaly-re-auth-with-oauth2/9125/3 "2019-11-20T18:25:36Z")

</div>

We also have a thread on this subject here:

> [@Medium Article: How to Automate OAuth2 Token Renewal in Postman](https://community.postman.com/t/medium-article-how-to-automate-oauth2-token-renewal-in-postman/6630):
>
> Hey everyone, I have seen on a number of forums and stack overflow articles people expressing their frustration with the manual process Postman has for renewing OAuth2 tokens when they expire. I thought I would share a write up I did to help you get past the manual process and have your tokens renewing automatically. Let me know what you think!

---

<div class="post-metadata">

**Author:** ![simfre](https://avatars.discourse-cdn.com/v4/letter/s/ba8739/32.png) [@simfre](https://community.postman.com/u/simfre)\
**Post date:** [November 21, 2019, 8:11am UTC](https://community.postman.com/t/automaticaly-re-auth-with-oauth2/9125/4 "2019-11-21T08:11:35Z")

</div>

Thanks for these hints. I’m surprised there isn’t just a refresh button, or something like pm.reauthorize() to call. Could it be implemented? Rather than reinventing the wheel with these scripts.
