Last active
August 14, 2020 05:01
-
-
Save rabingaire/051530ba512e155c3348e8c2cf32bdea to your computer and use it in GitHub Desktop.
Postman accesss token & refresh token get automation 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
const url = `${pm.environment.get("base_url")}/signin`; | |
const email = pm.environment.get("email"); | |
const password = pm.environment.get("password"); | |
const postRequest = { | |
url: url, | |
method: "POST", | |
header: "Content-Type:application/json", | |
body: { | |
mode: "application/json", | |
raw: JSON.stringify({ | |
email: email, | |
password: password, | |
}), | |
}, | |
}; | |
let getToken = true; | |
const accessToken = pm.environment.get("access_token"); | |
const idToken = pm.environment.get("identity_token"); | |
const expiresIn = pm.environment.get("expires_in"); | |
if (accessToken && idToken && expiresIn) { | |
if (expiresIn >= new Date().getTime()) { | |
getToken = false; | |
} | |
} | |
if (getToken) { | |
pm.sendRequest(postRequest, (err, res) => { | |
if (!err) { | |
var responseJson = res.json(); | |
pm.environment.set( | |
"access_token", | |
responseJson.data.access_token | |
); | |
pm.environment.set("identity_token", responseJson.data.id_token); | |
let expiryDate = new Date(); | |
expiryDate.setSeconds( | |
expiryDate.getSeconds() + responseJson.data.expires_in | |
); | |
pm.environment.set("expires_in", expiryDate.getTime()); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Variables required on postman collection