Created
May 3, 2021 13:54
-
-
Save iuricmp/9e979bc7c334c99e8b2962cc42bfc30a to your computer and use it in GitHub Desktop.
Postman OAuth2 - Basic Authentication
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 clientId = pm.variables.get('clientId'); | |
const oauthUser = pm.variables.get('oauthUser'); // user name or email | |
const oauthPassword = pm.variables.get('oauthPassword'); // user password | |
const scope = pm.variables.get('scope'); // "profile" | |
const accessTokenURL = pm.variables.get('accessTokenURL'); // token url | |
const getTokenRequest = { | |
method: 'POST', | |
url: accessTokenURL, | |
header: 'Content-Type:application/x-www-form-urlencoded', | |
body: { | |
mode: 'urlencoded', | |
urlencoded: [ | |
{ key: "client_id", value: clientId }, | |
{ key: "username", value: oauthUser }, | |
{ key: "password", value: oauthPassword }, | |
{ key: "grant_type", value: "password" } | |
] | |
} | |
}; | |
pm.sendRequest(getTokenRequest, (err, response) => { | |
const jsonResponse = response.json(), | |
newAccessToken = jsonResponse.access_token; | |
console.log({ err, jsonResponse, newAccessToken }) | |
pm.environment.set('OAuthToken', newAccessToken); | |
pm.variables.set('OAuthToken', newAccessToken); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment