Outlook IMAP OAuth in Frappe fails with "AUTHENTICATE failed" when fetching emails

Hi all,

I’m trying to integrate Outlook email (Microsoft 365) into Frappe using OAuth 2.0, but I’m consistently getting an AUTHENTICATE failed error when it tries to connect to IMAP.

Error Traceback:

Traceback with variables (most recent call last):
  File "apps/frappe/frappe/email/oauth.py", line 43, in connect
    self._connect_imap()
      self = <frappe.email.oauth.Oauth object at 0x7637f575c8b0>
  File "apps/frappe/frappe/email/oauth.py", line 71, in _connect_imap
    self._conn.authenticate(self._mechanism, lambda x: self._auth_string)
      self = <frappe.email.oauth.Oauth object at 0x7637f575c8b0>
  File "/usr/lib/python3.10/imaplib.py", line 444, in authenticate
    raise self.error(dat[-1].decode('utf-8', 'replace'))
      self = <imaplib.IMAP4_SSL object at 0x7637f575e590>
      mechanism = 'XOAUTH2'
      authobject = <function Oauth._connect_imap.<locals>.<lambda> at 0x7637f46a3400>
      mech = 'XOAUTH2'
      typ = 'NO'
      dat = [b'AUTHENTICATE failed.']
imaplib.error: AUTHENTICATE failed.

What’s Already Done:

  • Registered app in Azure Portal
  • Added delegated API permissions:
    • IMAP.AccessAsUser.All
    • SMTP.Send
    • offline_access, openid, email, profile
  • Granted admin consent
  • Scopes added and enabled (checked) in Frappe Connected App
  • Microsoft OAuth authorized successfully in Email Account
  • IMAP access enabled via Microsoft 365 Admin Center → Manage email apps
  • IMAP server config:
    • imap-mail.outlook.com
    • Port: 993
    • SSL: Enabled
  • Cleared Token Cache, re-authorized multiple times
  • Using Microsoft 365 (work)

SMTP Server: smtp.office365.com
Port: 587
Use TLS: Yes
Use SSL: No
Authentication Method: OAuth 2.0 (Microsoft Connected App)
Authorization: Done (clicked “Authorize Microsoft Access”)
Password: Not used (OAuth token used instead)

Environment:

  • Frappe Version: `Screenshots:
  • Hosted: AWS EC2
  • Python: 3.11
  • OAuth method: Microsoft (Connected App in Social Login Key)

Screenshots:



Email Account config:

Looking For:

  • Has anyone successfully integrated Outlook with OAuth in Frappe v15?
  • Any known patches or debug steps to verify token content or connection?

Thanks in advance!