Created
May 18, 2019 00:47
-
-
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
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
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