Skip to content

Instantly share code, notes, and snippets.

@TiMladenov
Created January 7, 2020 14:04
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 TiMladenov/20d3ff0de4e1e919d2546be847cd45a9 to your computer and use it in GitHub Desktop.
Save TiMladenov/20d3ff0de4e1e919d2546be847cd45a9 to your computer and use it in GitHub Desktop.
let userSecret = "your_user_secret";
let clientSecret = "your_client_secret";
let host_url = 'your_host_url';
let scope = 'your_app_scope';
let toEncode = userSecret +":"+ clientSecret;
toEncode = btoa(toEncode);
pm.environment.set("auth_token", toEncode);
pm.environment.set("host_url", host_url);
pm.environment.set("accessToken", "");
//edit URL to be the one of the token generator
const echoPostRequest = {
url: 'https://'+host_url+'/oauther/oauth2/token',
method: 'POST',
header: {
'Content-Type':'application/x-www-form-urlencoded',
'Host': host_url,
'Authorization' : 'Basic ' + toEncode
},
body: {
mode: 'raw',
raw: 'grant_type=client_credentials&scope=' + scope
}
};
pm.sendRequest(echoPostRequest, function (err, response) {
console.log(response.json());
let respJson = response.json();
if(respJson.access_token) {
let access_token = respJson.access_token;
pm.environment.set('accessToken', access_token);
pm.test("Access token available and saved.")
} else {
pm.test("Pre-script failure", function() {
throw new Error("Access token missing from response.");
})
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment