-
-
Save bcnzer/073f0fc0b959928b0ca2b173230c0669 to your computer and use it in GitHub Desktop.
const echoPostRequest = { | |
url: 'https://<my url>.auth0.com/oauth/token', | |
method: 'POST', | |
header: 'Content-Type:application/json', | |
body: { | |
mode: 'application/json', | |
raw: JSON.stringify( | |
{ | |
client_id:'<your client ID>', | |
client_secret:'<your client secret>', | |
audience:'<my audience>', | |
grant_type:'client_credentials' | |
}) | |
} | |
}; | |
var getToken = true; | |
if (!pm.environment.get('accessTokenExpiry') || | |
!pm.environment.get('currentAccessToken')) { | |
console.log('Token or expiry date are missing') | |
} else if (pm.environment.get('accessTokenExpiry') <= (new Date()).getTime()) { | |
console.log('Token is expired') | |
} else { | |
getToken = false; | |
console.log('Token and expiry date are all good'); | |
} | |
if (getToken === true) { | |
pm.sendRequest(echoPostRequest, function (err, res) { | |
console.log(err ? err : res.json()); | |
if (err === null) { | |
console.log('Saving the token and expiry date') | |
var responseJson = res.json(); | |
pm.environment.set('currentAccessToken', responseJson.access_token) | |
var expiryDate = new Date(); | |
expiryDate.setSeconds(expiryDate.getSeconds() + responseJson.expires_in); | |
pm.environment.set('accessTokenExpiry', expiryDate.getTime()); | |
} | |
}); | |
} |
@bcnzer - I am trying to figure out if similar script could work for auth code flow. I need user to sign in, based on which need to generate the access token. Unfortunately, postman "Authorization" tab does not expose the access_token as variable and they are still working on it (since long 4 years). Have you ever faced this situation? Do you have any workarounds in mind?
It works perfectly! Time & Effort saving
Thanks a lot
thx a lot,
I use it in postman pre-request script but unfortunately I couldn't pass the currentAccessToken to the second request
how to do it?
thx a lot, I use it in postman pre-request script but unfortunately I couldn't pass the currentAccessToken to the second request how to do it?
@sayuri-sam What do you mean, 2nd request? Have you specified the currentAccessToken
as a variable in the Authorization tab of the request?
bo55vxr
yes, I use this code as pre-request script in postman.
and I want to pass the value in currentAccessToken to Auth token.
can you figure it out?
@sayuri-sam you need to use double curly braces {{currentAccessToken}}
@sayuri-sam you need to use double curly braces
{{currentAccessToken}}
^^^ This...
@Solksjaer thanks for the snippet just what i needed 👍
Tks! 👍
Hi @anantyadunath. Quick question, did you ever get auth code flow working with Postman?
amazing!