600 字
3 分钟

获取 Outlook Refresh Token

2026-04-16
浏览量 加载中...

在使用 Outlook(Microsoft 365 / Hotmail)进行邮件自动化、IMAP/SMTP 登录或开发项目时,经常需要获取 Refresh Token。但很多在线工具容易出现 different client idinvalid_scope 等问题。

本文提供一个 纯浏览器 + curl 的稳定方法,无需注册应用、无需安装复杂工具,适合 macOS 用户。

适用场景#

  • 需要长期使用 Outlook IMAP / SMTP
  • 自动化收发邮件、获取验证码等
  • 不想自己注册 Azure App

使用到的 Client ID#

我们使用 Thunderbird 的公开 Client ID(社区最常用、最稳定):

Client ID: 9e5f94bc-e8a4-4e73-b8be-63364c29d753

详细操作步骤#

步骤 1:获取授权码(Authorization Code)#

  1. 打开 Safari 或 Chrome 浏览器。
  2. 完整复制下面这个链接并粘贴到地址栏打开:
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
  1. 使用你的 Outlook / Hotmail 账号登录。
  2. 点击 接受同意 所有请求的权限。
  3. 页面会跳转失败(显示无法连接 localhost,这是正常现象)。
  4. 立即 在地址栏找到 code= 后面的那一长串字符,完整复制(从 code= 开始,直到下一个 & 符号之前)。

⚠️ 注意:授权码有效期很短(约 5-10 分钟),请尽快进行下一步。

步骤 2:用授权码换取 Refresh Token#

  1. 打开 Mac 的 终端(Terminal)。
  2. 复制下面整条命令:
Terminal window
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'
  1. 把上面命令中的 你的授权码 替换为你刚刚复制的那一长串 code(注意不要加空格或额外引号)。
  2. 按回车执行。

步骤 3:查看返回结果#

如果操作成功,你会看到类似下面的 JSON 返回:

{
"token_type": "Bearer",
"scope": "...",
"expires_in": 3600,
"access_token": "EwAw...",
"refresh_token": "0.AwAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
...
}

重点复制

  • refresh_token 的值(以 0.AwA 开头的那一长串)
  • client_id9e5f94bc-e8a4-4e73-b8be-63364c29d753

常见问题与解决#

  1. zsh: event not found

    解决:确保所有 -d '内容' 都用单引号 ' 包裹。

  2. The code has expired

    解决:授权码过期了,重新执行步骤 1 获取新的 code,然后立即执行步骤 2。

  3. invalid_scope

    解决:确认 scope 只使用了 outlook.office.com 系列,不要和 graph.microsoft.com 混用。

  4. invalid_grant: different client id

    解决:必须使用上面固定的 Client ID,不能和在线工具混用。

后续使用建议#

  • client_idrefresh_token 安全保存。
  • Refresh Token 有效期通常为 90 天左右,建议定期刷新。
  • 在代码中使用时,推荐用 refresh_token 定期换取新的 access_token

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

获取 Outlook Refresh Token
https://printsdf.dpdns.org/posts/2026-04-16-获取-outlook-refresh-token/
作者
printsdf
发布于
2026-04-16
许可协议
CC BY-NC-SA 4.0

评论区

Profile Image of the Author
printsdf
Hello, I'm printsdf.
公告
欢迎来到我的博客!这是一则示例公告。
音乐
封面

音乐

暂未播放

0:00 0:00
暂无歌词
分类
标签
站点统计
文章
37
分类
12
标签
14
总字数
47,088
运行时长
0
最后活动
0 天前

目录