Pixiv - KiraraShss
600 字
3 分钟
获取 Outlook Refresh Token
在使用 Outlook(Microsoft 365 / Hotmail)进行邮件自动化、IMAP/SMTP 登录或开发项目时,经常需要获取 Refresh Token。但很多在线工具容易出现 different client id 或 invalid_scope 等问题。
本文提供一个 纯浏览器 + curl 的稳定方法,无需注册应用、无需安装复杂工具,适合 macOS 用户。
适用场景
- 需要长期使用 Outlook IMAP / SMTP
- 自动化收发邮件、获取验证码等
- 不想自己注册 Azure App
使用到的 Client ID
我们使用 Thunderbird 的公开 Client ID(社区最常用、最稳定):
Client ID: 9e5f94bc-e8a4-4e73-b8be-63364c29d753
详细操作步骤
步骤 1:获取授权码(Authorization Code)
- 打开 Safari 或 Chrome 浏览器。
- 完整复制下面这个链接并粘贴到地址栏打开:
https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=9e5f94bc-e8a4-4e73-b8be-63364c29d753&response_type=code&redirect_uri=https://localhost&response_mode=query&scope=offline_access%20https://outlook.office.com/IMAP.AccessAsUser.All%20https://outlook.office.com/POP.AccessAsUser.All%20https://outlook.office.com/SMTP.Send- 使用你的 Outlook / Hotmail 账号登录。
- 点击 接受 或 同意 所有请求的权限。
- 页面会跳转失败(显示无法连接 localhost,这是正常现象)。
- 立即 在地址栏找到
code=后面的那一长串字符,完整复制(从code=开始,直到下一个&符号之前)。
⚠️ 注意:授权码有效期很短(约 5-10 分钟),请尽快进行下一步。
步骤 2:用授权码换取 Refresh Token
- 打开 Mac 的 终端(Terminal)。
- 复制下面整条命令:
curl -X POST https://login.microsoftonline.com/common/oauth2/v2.0/token \ -d 'client_id=9e5f94bc-e8a4-4e73-b8be-63364c29d753' \ -d 'scope=offline_access https://outlook.office.com/IMAP.AccessAsUser.All https://outlook.office.com/POP.AccessAsUser.All https://outlook.office.com/SMTP.Send' \ -d 'code=你的授权码' \ -d 'grant_type=authorization_code' \ -d 'redirect_uri=https://localhost'- 把上面命令中的
你的授权码替换为你刚刚复制的那一长串 code(注意不要加空格或额外引号)。 - 按回车执行。
步骤 3:查看返回结果
如果操作成功,你会看到类似下面的 JSON 返回:
{ "token_type": "Bearer", "scope": "...", "expires_in": 3600, "access_token": "EwAw...", "refresh_token": "0.AwAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", ...}重点复制:
refresh_token的值(以0.AwA开头的那一长串)client_id:9e5f94bc-e8a4-4e73-b8be-63364c29d753
常见问题与解决
-
zsh: event not found
解决:确保所有
-d '内容'都用单引号'包裹。 -
The code has expired
解决:授权码过期了,重新执行步骤 1 获取新的 code,然后立即执行步骤 2。
-
invalid_scope
解决:确认 scope 只使用了
outlook.office.com系列,不要和graph.microsoft.com混用。 -
invalid_grant: different client id
解决:必须使用上面固定的 Client ID,不能和在线工具混用。
后续使用建议
- 将
client_id和refresh_token安全保存。 - Refresh Token 有效期通常为 90 天左右,建议定期刷新。
- 在代码中使用时,推荐用
refresh_token定期换取新的access_token。
文章分享
如果这篇文章对你有帮助,欢迎分享给更多人!
获取 Outlook Refresh Token
https://printsdf.dpdns.org/posts/2026-04-16-获取-outlook-refresh-token/
printsdf's Blog