Skip to content

Instantly share code, notes, and snippets.

@iuricmp
Created May 3, 2021 13:54
Show Gist options
  • Save iuricmp/9e979bc7c334c99e8b2962cc42bfc30a to your computer and use it in GitHub Desktop.
Save iuricmp/9e979bc7c334c99e8b2962cc42bfc30a to your computer and use it in GitHub Desktop.
Postman OAuth2 - Basic Authentication
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