Last active
August 20, 2020 14:11
-
-
Save paulsancer/4ad6a42ece32ce6b5ea4d01346ca3512 to your computer and use it in GitHub Desktop.
Use this JS script to automatically set the Graph Api access token instead of manually getting a new one every time it gets expired. Just put the `{{graphApiAccessToken}}` variable into the Auth tab by selecting type `Bearer Token`.
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 client_id = pm.environment.get('client_id'); | |
const client_secret = pm.environment.get('client_secret'); | |
const tenant_id = pm.environment.get('tenant_id'); | |
const graphApiAccessTokenTenantId = pm.globals.get( | |
'graphApiAccessTokenTenantId' | |
); | |
// If tenant_id from env is different from the already existing token, drop the vars | |
if (tenant_id !== graphApiAccessTokenTenantId) { | |
console.log('tenant_id changed, dropping existing global token...'); | |
pm.globals.unset('graphApiAccessToken'); | |
pm.globals.unset('graphApiAccessTokenExpiry'); | |
pm.globals.unset('graphApiAccessTokenTenantId'); | |
} | |
const graphApiAccessToken = pm.globals.get('graphApiAccessToken'); | |
const graphApiAccessTokenExpiry = pm.globals.get('graphApiAccessTokenExpiry'); | |
const echoPostRequest = { | |
url: `https://login.microsoftonline.com/${tenant_id}/oauth2/token`, | |
method: 'POST', | |
header: { 'Content-Type': 'application/x-www-form-urlencoded' }, | |
body: { | |
mode: 'urlencoded', | |
urlencoded: [ | |
{ key: 'client_id', value: client_id }, | |
{ key: 'client_secret', value: client_secret }, | |
{ key: 'grant_type', value: 'client_credentials' }, | |
{ key: 'scope', value: 'https://graph.microsoft.com/v1.0' } | |
] | |
} | |
}; | |
const currentTime = new Date().getTime(); | |
let validToken = false; | |
console.log(graphApiAccessToken ? `Token exists` : 'No token'); | |
console.log( | |
graphApiAccessTokenExpiry | |
? `Token expiry: ${graphApiAccessTokenExpiry}` | |
: 'No token expiry' | |
); | |
console.log(`Current time: ${currentTime}`); | |
if (!graphApiAccessToken || !graphApiAccessTokenExpiry) { | |
console.log('Missing token or expiry date.'); | |
} else if (graphApiAccessTokenExpiry <= currentTime) { | |
console.log('Token expired.'); | |
} else { | |
validToken = true; | |
console.log('Found valid token, reusing it.'); | |
} | |
if (!validToken) { | |
pm.sendRequest(echoPostRequest, function(err, res) { | |
console.log(err ? err : res.json()); | |
if (err === null) { | |
console.log('Saving the token and expiry date'); | |
const responseJson = res.json(); | |
pm.globals.set('graphApiAccessToken', responseJson.access_token); | |
const expiryDate = currentTime + responseJson.expires_in * 1000; | |
pm.globals.set('graphApiAccessTokenExpiry', expiryDate); | |
pm.globals.set('graphApiAccessTokenTenantId', tenant_id); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment