I have been following the tutorial posted here
https://learn.microsoft.com/en-us/outlook/rest/node-tutorial
to teach me how to access the Microsoft Graph API.
I have been working on my project at home and at work, and while the example works perfectly at home, I am unable to get an oAuth2 token while at work.
It all works up to this point
async function getTokenFromCode(auth_code) {
let result = await oauth2.authorizationCode.getToken({
code: auth_code,
redirect_uri: process.env.REDIRECT_URI,
scope: process.env.APP_SCOPES
});
const token = oauth2.accessToken.create(result);
console.log('Token created: ', token.token);
return token.token.access_token;
}
exports.getTokenFromCode = getTokenFromCode;
where the GET request for the token returns with status 200 but complains about an unhandled promise, so I have rewritten this section to be;
async function getTokenFromCode(auth_code, res) {
const tokenConfig = {
code: auth_code,
redirect_uri: process.env.REDIRECT_URI,
scope: process.env.APP_SCOPES
};
try {
let result = await oauth2.authorizationCode.getToken(tokenConfig);
const token = oauth2.accessToken.create(result);
saveValuesToCookie(token, res);
return token.token.access_token;
} catch (error){
console.log('Access Token Error:',error.message);
}
};
exports.getTokenFromCode = getTokenFromCode;
This now fails with "Client request error: ECONNREFUSED (some ip):443" where (some ip) changes every time but appears to be a Microsoft server e.g. 40.112.64.18:443
The credentials I am using for the request are as in the tutorial;
const credentials = {
client: {
id: process.env.APP_ID,
secret: process.env.APP_PASSWORD,
},
auth: {
tokenHost: 'https://login.microsoftonline.com',
authorizePath: 'common/oauth2/v2.0/authorize',
tokenPath: 'common/oauth2/v2.0/token'
}
};
const oauth2 = require('simple-oauth2').create(credentials);
I have tried doing the same request at work using Postman, following the setup here
Postman could not complete Oauth2 login for Microsoft Graph OAuth2
but the request hangs at a white screen after logging in.
As far as I know the proxy at work allows request from localhost (127.0.0.1) and I am able to request a code from the auth server so I am unsure why I am unable to request a token?
I have tried turning on proxy handling in express using;
app.set('trust proxy',true);
in the app.js but this does not seem to have any effect.
Any guidance will be gratefully received.