Home > Enterprise >  node-ews returning 401 Unauthorized where as using the valid access token
node-ews returning 401 Unauthorized where as using the valid access token

Time:10-06

I am using node-ews to fetch emails from the Microsoft Exchange server. It was working fine with basic auth.

But, as Microsoft disabled basic auth. We are currently using the OAuth token (access token) from Graph Explorer to test.

But it's returning 401 Unauthorised error.

This is the sample code we are using to connect to the exchange server.

const ewsConfig = {
            username: item.mail_username,
            password: item.user_pass,
            host: item.ews_host,
            token: 'xxxxxxxxxxx',
            auth: 'bearer'
          };

          // initialize node-ews
          const options = {
            rejectUnauthorized: false,
            strictSSL: false
          };
          // initialize node-ews
          const ews = new EWS(ewsConfig, options);

CodePudding user response:

. We are currently using the OAuth token (access token) from Graph Explorer to test.

The Graph Explorer token won't have permissions for EWS only Graph, the only two permission that are valid in EWS are EWS.AccessAsUser.All or full_access_as_app if using the client credentials flow. https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-authenticate-an-ews-application-by-using-oauth the Mail.Read etc permission don't work in EWS because it doesn't support the more restrictive authentication scheme that Graph supports (which is a reason to use the Graph over EWS)

If you want to accesstoken to test with use the EWSEditor https://github.com/dseph/EwsEditor/releases and grab its token

  • Related