Last active
October 12, 2020 04:22
-
-
Save celsogg/e59821a87cdf2674151a805a61f8c639 to your computer and use it in GitHub Desktop.
Plain Nodejs Microsoft Graph OAuth 2.0 client credentials grant flow
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 https = require('https'); | |
const querystring = require('querystring'); | |
const form = { | |
client_id: '<>', // a guid | |
client_secret: '<>', // an app secret | |
scope: 'https://graph.microsoft.com/.default', | |
grant_type: 'client_credentials' | |
}; | |
const formStr = querystring.stringify(form); | |
const tenant = ''; // xxxx.onmicrosoft.com | |
const options = { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': Buffer.byteLength(formStr) | |
} | |
}; | |
const req = https.request(`https://login.microsoftonline.com/${tenant}/oauth2/v2.0/token`, options, (res) => { | |
console.log('statusCode:', res.statusCode); | |
console.log('headers:', res.headers); | |
let responseData = ''; | |
res.on('data', (d) => { | |
responseData += d; | |
}); | |
res.on('end', () => { | |
console.log(responseData); | |
}); | |
}).on('error', (e) => { | |
console.error(e); | |
}); | |
req.write(formStr); | |
req.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment