gautamsi / ews-javascript-api

EWS API for TypeScript/JavaScript - ported from OfficeDev/ews-managed-api - node, cordova, meteor, Ionic, Electron, Outlook Add-Ins
MIT License
281 stars 72 forks source link

SoapFaultDetails 401 Unauthorized #391

Closed bistungki closed 7 months ago

bistungki commented 2 years ago

Hi Gautamsi,

i have a problem with your api, with the same config i tried fetch directly and it managed to read my inbox, for some reason after i moved Credentials from ews.ExchangeCredentials to ews.OAuthCredentials, it becomes a problem:

const msal = require('@azure/msal-node');

const main = async () => {
    const msalConfig = {
        auth: {
            clientId: "xx",
            authority: "https://login.microsoftonline.com/xx",
            clientSecret: "xx",
        }
    };
    const usernamePasswordRequest = {
        scopes: ["user.read"],
        username: "xx@xx.com", // Add your username here
        password: "123123", // Add your password here
    };
    const pca = new msal.PublicClientApplication(msalConfig);
    const getToken = async () => {
        return await pca.acquireTokenByUsernamePassword(usernamePasswordRequest)
    }
    const token = await getToken()
    console.log(token)
    var { ExchangeService, Uri, OAuthCredentials, ExchangeVersion, Folder, WellKnownFolderName, EwsLogging } = require("ews-javascript-api");
    EwsLogging.DebugLogEnabled = false;
    var exch = new ExchangeService(ExchangeVersion.Exchange2016);
    exch.Url = new Uri("https://outlook.office365.com/ews/exchange.asmx");
    exch.Credentials = new OAuthCredentials(token.accessToken);
    await Folder.Bind(exch, WellKnownFolderName.Inbox);
}

main()
SoapFaultDetails {
  message: '401 Unauthorized',
  InnerException: null,
  faultCode: null,
  faultString: null,
  faultActor: null,
  responseCode: 127,
  errorCode: 0,
  exceptionType: null,
  lineNumber: 0,
  positionWithinLine: 0,
  errorDetails: DictionaryWithStringKey {
    keys: [],
    keysToObjs: {},
    objects: {},
    keyPicker: [Function (anonymous)]
  },
  HttpStatusCode: 401,
  Exception: ServiceRequestUnauthorizedException {
    message: '401 Unauthorized',
    InnerException: null
  }
}

with the same config, I used the code from https://docs.microsoft.com/en-us/azure/active-directory/develop/tutorial-v2-nodejs-console and it worked. I really hope for your help. thank you very much

gautamsi commented 2 years ago

are you ablw to use same oauth token to call ews endpoint with hand crafted (ms example) xml?