Invalid Credentials error when connecting to Exchange Web Service

4.1k views Asked by At

I am trying to connect to the Exchange Web Service to get access to Outlook and Teams data using Exchangelib library in Python. I am following the below article, https://github.com/ecederstrand/exchangelib/issues/747

Below is the code snippet,

from exchangelib import IMPERSONATION, Account, Credentials, OAuth2Credentials, \
    Configuration, OAUTH2, Identity

client_id='XXX'
client_secret='XXX'
tenant_id='XXX'
user= '[email protected]'

credentials = OAuth2Credentials(
    client_id=client_id,
    client_secret=client_secret,
    tenant_id=tenant_id,
    identity=Identity(primary_smtp_address=user)
)
config = Configuration(
    credentials=credentials,
    auth_type=OAUTH2,
    service_endpoint="https://outlook.office365.com/EWS/Exchange.asmx",
)
account = Account(
    user,
    config=config,
    autodiscover=False,
    access_type=IMPERSONATION,
)
print(account.root.all().count())

I have given all the credentials correctly still, I am getting an Invalid Credential error as below.

    886     if response.status_code == 401 and protocol.retry_policy.fail_fast:
    887         # This is a login failure
--> 888         raise UnauthorizedError('Invalid credentials for %s' % response.url)
    889     if 'TimeoutException' in response.headers:
    890         raise response.headers['TimeoutException']

UnauthorizedError: Invalid credentials for https://outlook.office365.com/EWS/Exchange.asmx

Any solution is appreciated. Thanks in advance.

1

There are 1 answers

0
Cosmin Poieana On

Having the same problem exactly as described above and with auto-discovery on I'm getting: ErrorNonExistentMailbox: The SMTP address has no mailbox associated with it but it seems that's expected if you don't use impersonation.