Search code examples
python-3.xoutlookazure-active-directoryexchangewebservicesexchangelib

Access Outlook office mailbox(mails in inbox) using python code through EWS


I was reading the Outlook mails through EWS using python exchangelib module

credentials I was using for authentication: email & app password

Due to some security reasons the platform team have disabled the App password feature and I ended up accessing the mail box as am not sure how to access mail box without App password ???

If there is any alternative to App password please suggest me..

Note: My python script was running in Linux VM


Solution

  • When you use an email & app password this is using Basic Authentication which is why your platform team has disabled it. You need to move to using oAuth https://ecederstrand.github.io/exchangelib/#oauth-authentication and make use of the client_credentials flow https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow. So you then use a client secret or certificate rather then username & pword. When using this flow you need to make sure you impersonate the Mailbox your trying to access and also don't use Autodiscover because that will fail with that flow. By default this will give you access to all mailboxes in a tenant so if this is a problem you can scope it down to just the mailboxes you need to access https://techcommunity.microsoft.com/t5/exchange-team-blog/application-access-policy-support-in-ews/ba-p/2110361