Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh token automatically via Postman

Tags:

token

postman

When testing some API, the token need to be generated each 3min and I would like to do the refresh automatically, I did the following

I have a collection CollectionGetter which contain some requests

  • from CollectionGetter collection :
  1. I've added the following script in "Tests" tab

    var jsonData = pm.response.json();  
    pm.environment.set('getToken', jsonData.access_token);
    
  2. on authorisation tab, set:

    Type = Bearer token 
    Token {{getToken}}
    

then selected a request under CollectionGetter:

getAccount  (GET url/api/account)
Auth = inherit autho from parent

and sent it

=> got a 401 JSONError: No data, empty input at 1:1

Any help? Is my configuration correct?

like image 278
user281070 Avatar asked Sep 05 '25 14:09

user281070


2 Answers

Postman now has a feature that auto refresh auth tokens, you just need to setup authentication at the collection level and togle "the auto refresh access token" option: Image showing the auto refresh access token button

here's postman walktrough video about that feature, worth a watch: https://www.youtube.com/watch?v=FAhs9TBK-xU

TLDW:

1 - Click on your collection, it'll open the "auth" tab.

2 - Configure you Authentication type with Oauth 2.0

3 - Scroll to the bottom of the page and set your credentials and click "get new access token"

4 - Go up and togle the "Auto-refresh access token" option: Auto-refresh access token option

5 - With your token already generated go to your request, open the authorization tab of the request and set "type" to "inherit auth from parent", everything should work just fine.

Aditional resource: postman blog post about the feature

like image 139
Guilherme Dalonso Avatar answered Sep 09 '25 23:09

Guilherme Dalonso


*for testinging local in postman , Best method to set bearer token for all the request quick and easy way is, first you create a bearertoken with validity for more than 1 year(so it will not ask expired ) and copy ,and click on the postman collection top of the folder , if collection is

commonapi>1)loginapi >2)signupapi >3)homescreenapi

, like this format , click on top folder name commonapi, and there you can set bearertoken and paste the value , now in loginapi and all the below subfolder apis we can just set inherit auth from parent , so we dont need to create and set everytime bearer token again and again ,and it will save more time.*

like image 40
btm me Avatar answered Sep 09 '25 23:09

btm me