Last active
March 5, 2024 11:04
-
-
Save amitastreait/45f40305014df0e82a4ee3a4d62ba1f6 to your computer and use it in GitHub Desktop.
Get Refresh Token using the Postman Pre-Request Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(pm.response.code === 200 || pm.response.code === 201){ | |
var responseBody = pm.response.json(); | |
pm.environment.set('access_token', responseBody.access_token); | |
pm.environment.set('refresh_token', responseBody.refresh_token); | |
pm.environment.set('scope', responseBody.scope); | |
pm.environment.set('id_token', responseBody.id_token); | |
pm.environment.set('instance_url', responseBody.instance_url); | |
pm.environment.set('token_type', responseBody.token_type); | |
pm.environment.set('token_created_at', ( new Date() ).getTime() ); | |
var expiryDate = new Date(); | |
expiryDate.setSeconds(expiryDate.getSeconds() + 3600*2); | |
pm.environment.set('token_expires_at', expiryDate.getTime() ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const refreshTokenRequest = { | |
url: pm.collectionVariables.get('token_endpoint'), | |
method: 'POST', | |
header: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
}, | |
body: { | |
mode: 'urlencoded', | |
urlencoded: [ | |
{ key: "client_id", value: pm.environment.get('client_id') }, | |
{ key: "client_secret", value: pm.environment.get('client_secret') }, | |
{ key: "refresh_token", value: pm.environment.get('refresh_token') }, | |
{ key: "grant_type", value: "refresh_token" }, | |
] | |
} | |
}; | |
var getToken = true; | |
if ( !pm.environment.get('token_expires_at') ) { | |
console.log('Token or expiry date are missing'); | |
} else if (pm.environment.get('token_expires_at') <= (new Date()).getTime()) { | |
console.log('Token is expired') | |
} else { | |
getToken = false; | |
console.log('Token and expiry date are all good'); | |
console.log('Setting the Access token at header level ...'); | |
pm.request.headers.add({ | |
key: "Authorization", | |
value: "Bearer "+pm.environment.get('access_token') | |
}); | |
} | |
if (getToken === true) { | |
pm.sendRequest(refreshTokenRequest, function (err, res) { | |
var responseJson = res.json(); | |
if (err === null && responseJson.access_token) { | |
console.log('Saving the token and expiry date from refresh token API Call') | |
console.log( responseJson ); | |
pm.environment.set('access_token', responseJson.access_token) | |
var expiryDate = new Date(); | |
expiryDate.setSeconds(expiryDate.getSeconds() + 3600*2 ); | |
pm.environment.set('token_expires_at', expiryDate.getTime()); | |
console.log('Setting the Access token at header level ...'); | |
pm.request.headers.add({ | |
key: "Authorization", | |
value: "Bearer "+pm.environment.get('access_token') | |
}); | |
}else{ | |
console.log('Error while getting the refresh token '); | |
console.log( responseJson ); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment