Last active
April 3, 2019 18:58
-
-
Save wojtekk/d32ca526db4c1aeebd595992e592cb71 to your computer and use it in GitHub Desktop.
Postman
This file contains hidden or 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
function getVal(name) { | |
if (pm.variables.has(name)) { | |
return pm.variables.get(name); | |
} | |
if (pm.environment.has(name)) { | |
return pm.environment.get(name); | |
} | |
if (pm.globals.has(name)) { | |
return pm.globals.get(name); | |
} | |
} | |
const oauthUrl = getVal('OAUTH_URL'); | |
const clientId = getVal('OAUTH_CLIENT_ID'); | |
const clientSecret = getVal('OAUTH_CLIENT_SECRET'); | |
const scope = getVal('OAUTH_SCOPE'); | |
const resource = getVal('OAUTH_RESOURCE'); | |
const basicAuth = btoa(`${clientId}:${clientSecret}`); | |
const payload = { | |
url: `${oauthUrl}/oauth/token`, | |
method: 'POST', | |
header: { | |
Accept: 'application/json', | |
Authorization: `Basic ${basicAuth}`, | |
'Content-Type': 'application/x-www-form-urlencoded' | |
}, | |
body: { | |
mode: 'urlencoded', | |
urlencoded: [{ | |
key: 'grant_type', | |
value: 'client_credentials', | |
disabled: false | |
}] | |
} | |
}; | |
if (scope) { | |
payload.body.urlencoded.push({ | |
key: 'scope', | |
value: scope, | |
disabled: false | |
}); | |
} | |
if (resource) { | |
payload.body.urlencoded.push({ | |
key: 'resource', | |
value: resource, | |
disabled: false | |
}); | |
} | |
pm.sendRequest(payload, function(err, res) { | |
pm.expect(res).to.have.status(200); | |
pm.expect(res).to.have.jsonBody('access_token'); | |
console.info('New oAuth token fetched'); | |
const resObj = res.json(); | |
pm.variables.set("OAUTH_TOKEN", resObj.access_token); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment