Skip to content

Instantly share code, notes, and snippets.

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 RayGHeld/9bb52b6f4c4df275a7b6350e1a10fbd4 to your computer and use it in GitHub Desktop.
Save RayGHeld/9bb52b6f4c4df275a7b6350e1a10fbd4 to your computer and use it in GitHub Desktop.
PreRequest Script for Postman to get an access token for a resouce using the client credentials flow
var client_id = pm.environment.get("client_id");
var client_sercret = pm.environment.get("client_secret");
var token_endpoint = pm.environment.get("token_endpoint");
var scope = pm.environment.get("scope");
pm.sendRequest({
url: token_endpoint,
method: 'POST',
header: {
'Content-Type': 'multipart/form-data',
},
body: {
mode: 'formdata',
formdata: [
{key: "grant_type" , value: "client_credentials"},
{key: "client_id" , value: client_id},
{key: "client_secret" , value: client_sercret},
{key: "scope" , value: scope}
]
}
}, function(err, response) {
const jsonResponse = response.json();
console.log(jsonResponse);
pm.environment.set("access_token", jsonResponse.access_token);
console.log(pm.environment.get("access_token"));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment