trying to put together a simple python script to send emails from an office 365 mailbox using exchangelib, but cannot seem to get the authentication going. followed the steps in the documentation to register the app using "Delegated permissions" without any success. error is:
Exception has occurred: UnauthorizedError
Invalid credentials for https://autodiscover-s.outlook.com/autodiscover/autodiscover.svc
File "C:\Users\sfili\Documents\GitHub\newpythonscripts\V2023\email via ews\email_ewsv30_july_3.py", line 14, in <module>
account = Account(primary_smtp_address='xxx@yyy.com', credentials=credentials,autodiscover=True, access_type=IMPERSONATION)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
exchangelib.errors.UnauthorizedError: Invalid credentials for https://autodiscover-s.outlook.com/autodiscover/autodiscover.svc
below is the code snippet i am using, the above error is consistent even if access_type=DELEGATE is set
from exchangelib import DELEGATE, IMPERSONATION, Account, Credentials, EWSDateTime, EWSTimeZone, Configuration, NTLM, GSSAPI, CalendarItem, Message, Mailbox, Attendee, Q, ExtendedProperty, FileAttachment, ItemAttachment, HTMLBody, Build, Version, FolderCollection, OAuth2Credentials
credentials = OAuth2Credentials(
client_id='xxxx',
client_secret='xxxx',
tenant_id='xxxx'
)
account = Account(primary_smtp_address='xxx@yyy.com', credentials=credentials,autodiscover=True, access_type=IMPERSONATION)
for item in account.inbox.all().order_by("-datetime_received")[:10]:
print(item.subject, item.sender, item.datetime_received)