I’m working on a Python script to access two of my O365 mailboxes. One mailbox has multi-factor authentication (MFA) enabled, and the other doesn’t. I’ve successfully implemented access for the MFA-enabled mailbox. However, I’m facing challenges accessing the mailbox without MFA using user credentials (username/password) along with client ID and tenant ID.
Working code for the mailbox with MFA:
from exchangelib import Configuration, OAuth2Credentials, Account, DELEGATE, Build, Version, Identity, OAUTH2
username = ""
directory_id = ""
application_id = ""
client_secret = ""
email_subject = ""
credentials = OAuth2Credentials(client_id=application_id, client_secret=client_secret, tenant_id=directory_id, identity=Identity(primary_smtp_address=username))
version = Version(build=Build(15, 0, 12, 34))
config = Configuration(server='outlook.office365.com', credentials=credentials, version=version, auth_type=OAUTH2)
a = Account(primary_smtp_address=username, config=config, autodiscover=False, access_type=DELEGATE)
filtered_items = a.inbox.filter(subject__contains=email_subject)
count = filtered_items.count()
print(count)
I want to achieve similar access for the mailbox without MFA.
The problem is that the mailbox without MFA doesn’t have a client secret.
Thanks!
I’ve searched through various libraries like msal and O365, but I haven’t been able to find a clear implementation for non-MFA authentication using username, password, client ID and tenant ID.
Fun TJ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.