Gateway Token Doctor
Diagnose and fix 401 errors from token mismatches.
Problem
Gateway token inconsistencies cause:
- - 401 Unauthorized errors
- CLI/UI authentication failures
- Service startup failures
- Silent auth degradation
Workflow
1. Token Audit
CODEBLOCK0
2. Alignment Fix
CODEBLOCK1
3. Verification
CODEBLOCK2
Executable Completion Criteria
| Criteria | Verification |
|---|
| All tokens aligned | auth == remote == service |
| Gateway responds |
openclaw gateway status succeeds |
| CLI auth works |
openclaw whoami returns user |
| No 401 in logs |
Select-String "401" logs returns nothing |
Privacy/Safety
- - Never log actual token values
- Redact tokens in output (show first 4 chars only)
- Store tokens only in config files
Self-Use Trigger
Use when:
- - 401 errors appear
- Gateway restart after config change
- CLI shows auth mismatch
- Service fails to start
Align tokens. Restore access.
网关令牌诊断工具
诊断并修复因令牌不匹配导致的401错误。
问题
网关令牌不一致会导致:
- - 401未授权错误
- CLI/UI身份验证失败
- 服务启动失败
- 静默身份验证降级
工作流程
1. 令牌审计
powershell
检查所有令牌来源
$cfg = Get-Content $HOME/.openclaw/openclaw.json -Raw | ConvertFrom-Json
$auth = $cfg.gateway.auth.token
$remote = $cfg.gateway.remote.token
$service = $env:OPENCLAW
GATEWAYTOKEN
auth.token = $auth
remote.token = $remote
service.token = $service
if ($auth -and $remote -and $auth -ne $remote) {
Write-Warning 令牌不匹配:auth != remote
}
2. 对齐修复
powershell
生成或使用现有令牌
$token = $auth
更新配置
$cfg.gateway.auth.token = $token
$cfg.gateway.remote.token = $token
$cfg | ConvertTo-Json -Depth 10 | Out-File $HOME/.openclaw/openclaw.json -Encoding UTF8
更新服务启动脚本
$servicePath = $HOME/.openclaw/gateway.cmd
$content = Get-Content $servicePath -Raw
$content = $content -replace OPENCLAW
GATEWAYTOKEN=.*, OPENCLAW
GATEWAYTOKEN=$token
$content | Out-File $servicePath -Encoding UTF8
重启
openclaw gateway restart
3. 验证
powershell
测试网关访问
openclaw gateway status
测试CLI身份验证
openclaw whoami
可执行完成标准
| 标准 | 验证方式 |
|---|
| 所有令牌已对齐 | auth == remote == service |
| 网关响应正常 |
openclaw gateway status 执行成功 |
| CLI身份验证正常 | openclaw whoami 返回用户信息 |
| 日志中无401错误 | Select-String 401 logs 无返回结果 |
隐私/安全
- - 切勿记录实际令牌值
- 在输出中脱敏令牌(仅显示前4个字符)
- 令牌仅存储在配置文件中
使用触发条件
以下情况时使用:
- - 出现401错误
- 配置更改后重启网关
- CLI显示身份验证不匹配
- 服务启动失败
对齐令牌。恢复访问。