Here is the Python script I currently have:
def get_access_token():
tenantID = '123'
authority = 'https://login.microsoftonline.com/' + tenantID
clientID = '123'
clientSecret = '123'
scope = ['https://outlook.office365.com/.default']
app = msal.ConfidentialClientApplication(clientID,
authority=authority,
client_credential = clientSecret)
access_token = app.acquire_token_for_client(scopes=scope)
return access_token
def generate_auth_string(user, token):
auth_string = f"user={user}\1auth=Bearer {token}\1\1"
return auth_string
#IMAP AUTHENTICATE
imap = imaplib.IMAP4_SSL('outlook.office365.com', 993)
imap.debug = 4
access_token = get_access_token()
print(access_token['access_token'])
imap.authenticate("XOAUTH2", lambda x:generate_auth_string(
'[email protected]',
access_token['access_token']))
imap.select('inbox')
Here is the error:
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2288.0_x64__qbz5n2kfra8p0\Lib\imaplib.py", line 444, in authenticate
raise self.error(dat[-1].decode('utf-8', 'replace'))
imaplib.IMAP4.error: AUTHENTICATE failed.
Here is the debug information:
01:22.00 > b'NAFA1 AUTHENTICATE XOAUTH2'
01:22.03 < b'+ '
01:22.04 write literal size 2152
01:24.37 < b'NAFA1 NO AUTHENTICATE failed.'
01:24.37 NO response: b'AUTHENTICATE failed.'
I have verified that all of the information I'm passing is correct. I can also verify that the access token is being generated and passed through correctly. Could this be an encoding issue on the access_token string?