Skip to content

Instantly share code, notes, and snippets.

@rabingaire
Last active August 14, 2020 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rabingaire/051530ba512e155c3348e8c2cf32bdea to your computer and use it in GitHub Desktop.
Save rabingaire/051530ba512e155c3348e8c2cf32bdea to your computer and use it in GitHub Desktop.
Postman accesss token & refresh token get automation script
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());
}
});
}
@rabingaire
Copy link
Author

Variables required on postman collection

base_url
email
password
access_token
identity_token
expires_in

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment