title: Express Js using simple-oauth2 to access Microsoft Graph, possible proxy issue?
body: <p>I have been following the tutorial posted here</p>
  
  <p><a href="https://docs.microsoft.com/en-us/outlook/rest/node-tutorial" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/outlook/rest/node-tutorial</a></p>
  
  <p>to teach me how to access the Microsoft Graph API.</p>
  
  <p>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.</p>
  
  <p>It all works up to this point</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>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;</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>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</p>
  
  <p>The credentials I am using for the request are as in the tutorial;</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>I have tried doing the same request at work using Postman, following the setup here</p>
  
  <p><a href="https://stackoverflow.com/questions/49803861/postman-could-not-complete-oauth2-login-for-microsoft-graph-oauth2">Postman could not complete Oauth2 login for Microsoft Graph OAuth2</a></p>
  
  <p>but the request hangs at a white screen after logging in.</p>
  
  <p>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?</p>
  
  <p>I have tried turning on proxy handling in express using;</p>
  
  <pre><code>app.set('trust proxy',true);
  </code></pre>
  
  <p>in the app.js but this does not seem to have any effect.</p>
  
  <p>Any guidance will be gratefully received.</p>