Skip to content

Commit 87bc332

Browse files
authored
fix(imap): oauth2 url fix for outlook (#1701)
## What? - Added try-catch block to make it easier to debug OAuth2 errors encountered while authenticating IMAP. - Revised Outlook URL generation code for updated Entra ID redirect ## Why? I wasn't able to authenticate my Outlook emails previously. I'm not fully sure if the documentation clearly states that you need your Entra ID redirect to be configured as "Web" rather than "Mobile & Desktop App," but that is also a needed fix.
1 parent 1be6dfa commit 87bc332

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

config/oauth_script.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
"token_endpoint": "https://login.microsoftonline.com/common/oauth2/v2.0/token",
5858
"revoke_endpoint": None, # Microsoft does not support token revocation via endpoint
5959
"scopes": [
60-
"https://outlook.office365.com/IMAP.AccessAsUser.All",
61-
"https://outlook.office365.com/SMTP.Send",
60+
"https://outlook.office.com/IMAP.AccessAsUser.All",
61+
"https://outlook.office.com/SMTP.Send",
6262
"offline_access",
6363
],
6464
"extra_auth_params": {
@@ -171,8 +171,14 @@ def exchange_code(code, client_id, client_secret, provider):
171171
req = urllib.request.Request(cfg["token_endpoint"], data=data, method="POST")
172172
req.add_header("Content-Type", "application/x-www-form-urlencoded")
173173

174-
with urllib.request.urlopen(req) as resp:
175-
return json.loads(resp.read().decode())
174+
try:
175+
with urllib.request.urlopen(req) as resp:
176+
return json.loads(resp.read().decode())
177+
except urllib.error.HTTPError as e:
178+
body = e.read().decode()
179+
print(f"Token exchange failed (HTTP {e.code}):", file=sys.stderr)
180+
print(body, file=sys.stderr)
181+
sys.exit(1)
176182

177183

178184
def refresh_access_token(refresh_token, client_id, client_secret, provider):

0 commit comments

Comments
 (0)