Skip to content

Instantly share code, notes, and snippets.

@vipulgupta2048
Last active April 8, 2021 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vipulgupta2048/917a87dba26d153ae276eb1f218a8d8c to your computer and use it in GitHub Desktop.
Save vipulgupta2048/917a87dba26d153ae276eb1f218a8d8c to your computer and use it in GitHub Desktop.
Request to fetch AD token for the Immersive Reader using request
// Making a request to get the token with access to the .env file we created back in step 2
// One can wrap this into a route/endpoint for the frontend to query before starting the Immersive Reader resource
// router.get('/GetTokenAndSubdomain', function(req, res) {
request.post({
headers: { 'content-type': 'application/x-www-form-urlencoded' },
url: `https://login.windows.net/${process.env.TENANT_ID}/oauth2/token`,
form: {
grant_type: 'client_credentials',
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
resource: 'https://cognitiveservices.azure.com/'
}
},
function (err, res, tokenResult) {
if (err) {
console.log(err);
return res.status(500).send('Cognitive Services token error, did not receive the token');
}
var tokenResultParsed = JSON.parse(tokenResult);
if (tokenResultParsed.error) {
console.log(tokenResult);
return res.send({ error: "Unable to acquire Azure AD token. Please check if your .env values are correct." })
}
var token = tokenResultParsed.access_token;
var subdomain = process.env.SUBDOMAIN;
return res.send({ token, subdomain });
});
// })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment