pythonoutlookexchangewebservicesexchangelib

Exchangelib wrong credentials/request backoff after migration


Before showing you the error there is some backstory: Until a couple days ago the code worked just fine. Then they did a migration, before to login we used a link like "https://webmail.domain.it/owa/shared_mailbox@domain.it" now they migrated on cloud using the link "https://outlook.office365.com/mail".

the code is pretty straight:

class MyProxyAdapter(requests.adapters.HTTPAdapter):
    def send(self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None):
        proxies = {
            'http': 'proxy',
            'https': 'proxy',
        }
        return super().send(request, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)
    
def getAccount():
    try:
        user= "UT-*****-DEV"
        psw = "Password"

        BaseProtocol.HTTP_ADAPTER_CLS = MyProxyAdapter
        config = Configuration(server='outlook.office365.com',
          retry_policy=FaultTolerance(max_wait=900), credentials=Credentials(user,  psw)
        )
        return Account(
            'shared_mailbox@domain.it',
            config=config,
            autodiscover=True, access_type=DELEGATE
        )
    except Exception as e:
        print(f"Get data error: {e}...")
        return None

Before the migration this code worked just fine. Now it's throwing "wrong credentials". I've tried to deactive the autodiscover feature and instead of throwing wrong credentials the command account.protocol.version gives a warning: WARNING:exchangelib.util:Server requested back off until 2025-07-31 08:42:34.617995. Sleeping 9.998383 seconds. I've also tried to log in using "UT-XXX-DEV@domain.it" instead of "UT-XXX-DEV" but it still doesn't work. I've also tried to force the proxy using os.environ or using NoVerifyHTTPAdapter as the protocol but nothing changes.

Do someone have any tips or had this problem before?


Solution

  • Basic (username/password) authentication is no longer supported on office365.com. You need to set up OAuth, as described in https://ecederstrand.github.io/exchangelib/#impersonation-oauth-on-office-365