Created
May 17, 2019 19:48
-
-
Save rbipin/14141a4a2502f13827ea5d1ce9b13ec1 to your computer and use it in GitHub Desktop.
Postman Prerequest to get the oauth token and send it with actual request
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
getToken=true; | |
var envlevel=pm.environment.get('env'); | |
var userName=''; | |
var password=''; | |
var auth_audience=''; | |
var auth_scope=''; | |
//console.log(envlevel); | |
switch(envlevel){ | |
case 'local': | |
userName=pm.variables.get("auth_local_userName"); | |
password=pm.variables.get('auth_local_password'); | |
auth_scope=pm.variables.get('auth_local_scope'); | |
auth_audience=pm.variables.get('auth_local_audience'); | |
break; | |
case 'dev': | |
userName=pm.variables.get("auth_dev_userName"); | |
password=pm.variables.get('auth_dev_password'); | |
auth_scope=pm.variables.get('auth_dev_scope'); | |
auth_audience=pm.variables.get('auth_dev_audience'); | |
break; | |
case 'test': | |
userName=pm.variables.get("auth_test_userName"); | |
password=pm.variables.get('auth_test_password'); | |
auth_scope=pm.variables.get('auth_test_scope'); | |
auth_audience=pm.variables.get('auth_test_audience'); | |
break; | |
case 'beta': | |
userName=pm.variables.get("auth_beta_userName"); | |
password=pm.variables.get('auth_beta_password'); | |
auth_scope=pm.variables.get('auth_beta_scope'); | |
auth_audience=pm.variables.get('auth_beta_audience'); | |
break; | |
case 'prod': | |
userName=pm.variables.get("auth_prod_userName"); | |
password=pm.variables.get('auth_prod_password'); | |
auth_scope=pm.variables.get('auth_prod_scope'); | |
auth_audience=pm.variables.get('auth_prod_audience'); | |
break; | |
} | |
//console.log(userName); | |
//console.log(password); | |
//console.log(auth_scope); | |
//console.log(auth_audience); | |
var CryptoJS = require("crypto-js"); | |
var combinedCredentials=userName+":"+password; | |
var credentialsRaw=CryptoJS.enc.Utf8.parse(combinedCredentials); | |
var base64EncodedCredentials= CryptoJS.enc.Base64.stringify(credentialsRaw); | |
//console.log(combinedCredentials); | |
//console.log(base64EncodedCredentials); | |
const postTokenRequest = { | |
url: pm.environment.get('auth_token_url')+'?grant_type=' | |
+pm.environment.get('auth_grant_type') | |
+"&aud="+auth_audience | |
+"&scope="+auth_scope, | |
method: 'POST', | |
header: { | |
'Content-Type':'application/json', | |
'Authorization':'Basic '+base64EncodedCredentials | |
} | |
}; | |
if (!pm.variables.get('tokenExpiry') || | |
!pm.variables.get('currentAccessToken')) { | |
console.log('Token or expiry date are missing'); | |
} else if (pm.variables.get('tokenExpiry') <= (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(postTokenRequest, 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.variables.set('currentAccessToken', responseJson.access_token); | |
//console.log("currentAccessToken: "+ pm.variables.get('currentAccessToken')); | |
var expiryDate = new Date(); | |
expiryDate.setSeconds(expiryDate.getSeconds() + responseJson.expires_in); | |
pm.variables.set('tokenExpiry', expiryDate.getTime()); | |
//console.log("tokenExpiry: "+pm.variables.get('tokenExpiry')); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment