Skip to content

Instantly share code, notes, and snippets.

@josbelvivial
Created February 17, 2020 14:37
Show Gist options
  • Save josbelvivial/eac9847223dead18da77c54bc3344384 to your computer and use it in GitHub Desktop.
Save josbelvivial/eac9847223dead18da77c54bc3344384 to your computer and use it in GitHub Desktop.
get-postman-token
function getIdToken() {
const access_token = pm.variables.get('access_token');
if (!access_token) {
console.log(`access_token: ${access_token}`)
}
const auth0_client_id = pm.variables.get('auth0_client_id');
const client_secret = pm.variables.get('client_secret');
const auth0_domain = pm.variables.get('auth0_domain');
const user_name = pm.variables.get('user_name');
const password = pm.variables.get('password');
const idTokenPayload = {
url: `https://${auth0_domain}/oauth/token`,
method: 'POST',
header: 'Content-Type:application/json',
body: {
mode: 'application/json',
raw: JSON.stringify(
{
client_id: auth0_client_id,
client_secret: client_secret,
username: user_name,
password: password,
grant_type:'password'
})
}
};
pm.sendRequest(idTokenPayload, function (err, res) {
if (err === null) {
var responseJson = res.json();
console.log(responseJson);
getAccessToken(responseJson.id_token);
}
});
}
function getAccessToken(id_token) {
const authorization_grant_type = pm.variables.get('authorization_grant_type');
const api_url = pm.variables.get('api_url');
const accessTokenPayload = {
url: `${api_url}/token`,
method: 'POST',
header: 'Content-Type:application/x-www-form-urlencoded',
body: {
mode: 'urlencoded',
urlencoded:
[
{key: 'assertion', value: id_token},
{key: 'grant_type', value: authorization_grant_type}
]
}
};
pm.sendRequest(accessTokenPayload, function (err, res) {
if (err === null) {
var responseJson = res.json();
console.log(responseJson);
pm.variables.set('access_token', responseJson.access_token);
}
});
}
pm.variables.set('getIdToken', getIdToken);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment