OAuth
OpenClaw 通过 OAuth 支持“订阅认证”(支持该方式的 providers,尤其是 OpenAI Codex(ChatGPT OAuth))。Anthropic 订阅请使用 setup-token 流程。本文解释:- OAuth token exchange 如何工作(PKCE)
- tokens 存储在哪里(以及原因)
- 如何处理多账号(profiles + per-session 覆盖)
Token sink(为何存在)
OAuth providers 通常在登录/刷新时生成新的 refresh token。部分 providers(或 OAuth 客户端)会在同一用户/应用生成新 token 时使旧 token 失效。 实际症状:- 你同时用 OpenClaw 和 Claude Code / Codex CLI 登录 → 其中一个会在之后“随机掉线”
auth-profiles.json 视为 token sink:
- 运行时只从一个地方读取凭据
- 可保留多个 profile 并进行确定性路由
存储(token 在哪里)
Secrets 按 agent 存储:- Auth profiles(OAuth + API keys):
~/.openclaw/agents/<agentId>/agent/auth-profiles.json - 运行时缓存(自动管理,不要编辑):
~/.openclaw/agents/<agentId>/agent/auth.json
~/.openclaw/credentials/oauth.json(首次使用时导入auth-profiles.json)
$OPENCLAW_STATE_DIR 覆盖。完整参考:/gateway/configuration
Anthropic setup-token(订阅认证)
在任意机器运行claude setup-token,然后粘贴到 OpenClaw:
OAuth exchange(登录如何工作)
OpenClaw 的交互式登录流程由@mariozechner/pi-ai 实现,并接入向导/命令。
Anthropic(Claude Pro/Max)setup-token
流程:- 运行
claude setup-token - 粘贴 token 到 OpenClaw
- 作为 token auth profile 存储(不刷新)
openclaw onboard → auth choice setup-token(Anthropic)。
OpenAI Codex(ChatGPT OAuth)
流程(PKCE):- 生成 PKCE verifier/challenge + 随机
state - 打开
https://auth.openai.com/oauth/authorize?... - 尝试在
http://127.0.0.1:1455/auth/callback捕获回调 - 若回调无法绑定(或你在远程/无头环境),粘贴 redirect URL/code
- 在
https://auth.openai.com/oauth/token交换 - 从 access token 提取
accountId并存储{ access, refresh, expires, accountId }
openclaw onboard → auth choice openai-codex。
刷新 + 过期
Profiles 存储expires 时间戳。
运行时:
expires在未来 → 使用已存储的 access token- 已过期 → 刷新(加文件锁)并覆盖已存储的凭据
多账号(profiles)+ 路由
两种模式:1) 推荐:分离 agents
若希望 “personal” 与 “work” 永不交叉,请使用隔离 agents(独立 sessions + credentials + workspace):2) 高级:单 agent 多 profiles
auth-profiles.json 可为同一 provider 存储多个 profile ID。
选择使用哪个 profile:
- 全局:通过配置顺序(
auth.order) - 会话级:通过
/model ...@<profileId>
/model Opus@anthropic:work
openclaw channels list --json(显示auth[])
- /concepts/model-failover(轮换 + 冷却规则)
- /tools/slash-commands(命令面)