Skip to content

Instantly share code, notes, and snippets.

@loic-roux-404
Last active April 13, 2021 14:06
Show Gist options
  • Save loic-roux-404/71da8c68622c7e1053ef699a54225ae5 to your computer and use it in GitHub Desktop.
Save loic-roux-404/71da8c68622c7e1053ef699a54225ae5 to your computer and use it in GitHub Desktop.
const defaults = {
// username: '',
grant_type: 'client_credentials',
tokenRoute: "oauth/v2/token"
}
const credentials = {
dev: {
url: 'http://',
client_id: '',
client_secret: ''
},
int: {
url: 'http://',
client_id: '',
client_secret: ''
},
recette: {
url: 'http://',
client_id: '',
client_secret: ''
},
preprod: {
url: 'http://',
client_id: '',
client_secret: ''
},
prod: {
url: 'http://',
client_id: '',
client_secret: ''
},
}
const formdata = {
...credentials[pm.environment.name || 'int'],
...defaults
}
const { url, tokenRoute } = formdata
pm.variables.set('endpoint', url);
delete formdata['url']
delete formdata['tokenRoute']
const getTokenRequest = {
method: 'POST',
url: `${url}/${tokenRoute}`,
body: {
mode: 'formdata',
formdata: Object.entries(formdata).map(([k, v]) => ({'key': k, 'value': v}))
}
};
pm.sendRequest(getTokenRequest, (err, response) => {
const {code, status} = response
if (code > 400) {
throw new Error(JSON.stringify({code, status}))
}
const jsonResponse = response.json();
const newAccessToken = jsonResponse.access_token;
pm.variables.set('access_token', newAccessToken);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment