Last active
April 13, 2021 14:06
-
-
Save loic-roux-404/71da8c68622c7e1053ef699a54225ae5 to your computer and use it in GitHub Desktop.
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 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