Claude OAuth Auto-Renewal
简介
Claude OAuth Auto-Renewal
原始描述
Automatically detect and renew expired Claude Code OAuth tokens via heartbeat. 3-tier renewal: refresh token → Chrome browser automation → user alert.
通过你的 Agent 添加
安装目录:claude-oauth-renewal/(目录名取自 SKILL.md 的 frontmatter name,不是 URL 里的 slug)。
点「快速安装」复制一段指令贴给你的 Agent:它会先下载到 /tmp、把文件清单和校验值给你看,你确认之后才解压安装。不会一步装完、也不改系统其它位置。
或者手动安装
包是「根级 SKILL.md + 作者附带的文件」,解压到上面的 <slug>/ 目录即可;
目录名不用换,也不要把包直接解压到 skills 根目录。
技能申请的权限由作者决定:装之前先看清单里有没有脚本、它会读写什么。
技能正文
name: claude-oauth-renewal
description: "Automatically detect and renew expired Claude Code OAuth tokens via heartbeat. 3-tier renewal: refresh token → Chrome browser automation → user alert."
homepage: https://github.com/anthropics/claude-code
metadata: { "openclaw": { "emoji": "🔑", "requires": { "bins": ["claude", "security", "python3"], "platform": "macos" } } }
Claude Code OAuth Auto-Renewal
Automatically detect and renew expired Claude Code OAuth tokens during OpenClaw heartbeat cycles. Prevents agent downtime caused by token expiration.
When to Use
✅ USE this skill when:
- Your OpenClaw agent uses Claude Code as the AI provider
- You want uninterrupted agent operation without manual token renewal
- You're running OpenClaw on macOS with Chrome browser
How It Works
3-Tier Renewal Strategy
Heartbeat triggers check-claude-oauth.sh
│
├─ Token healthy (>6h remaining) → silent exit ✓
│
├─ Tier 1: claude auth status (refresh token)
│ ├─ Success → silent exit ✓
│ └─ Fail ↓
│
├─ Tier 2: Browser automation (osascript + Chrome JXA)
│ ├─ Start claude auth login
│ ├─ Auto-click "Authorize" on claude.ai
│ ├─ Extract auth code from callback page
│ ├─ Feed code back to CLI via expect
│ ├─ Success → silent exit ✓
│ └─ Fail ↓
│
└─ Tier 3: Alert user → agent notifies via configured channel
Token Storage
Claude Code stores OAuth tokens in macOS Keychain under the service name Claude Code-credentials. The token JSON includes:
accessToken— API access token (prefixsk-ant-oat01-)refreshToken— Used for automatic renewal (prefixsk-ant-ort01-)expiresAt— Unix timestamp in milliseconds
Prerequisites
- macOS with
security CLI (Keychain access)
- Claude Code installed and previously authenticated
- Google Chrome with
View → Developer → Allow JavaScript from Apple Events enabled (for Tier 2)
- python3 available in PATH
- expect available (ships with macOS)
Setup
1. Copy the script
cp skills/claude-oauth-renewal/scripts/check-claude-oauth.sh scripts/check-claude-oauth.sh
chmod +x scripts/check-claude-oauth.sh
2. Add to HEARTBEAT.md
security CLI (Keychain access)View → Developer → Allow JavaScript from Apple Events enabled (for Tier 2)1. Copy the script
cp skills/claude-oauth-renewal/scripts/check-claude-oauth.sh scripts/check-claude-oauth.sh
chmod +x scripts/check-claude-oauth.sh
Add as the first step in your heartbeat execution:
## Execution Order
0. Run `bash scripts/check-claude-oauth.sh` — if output exists, relay as highest priority alert
1. (your other heartbeat checks...)
3. Test
# Normal check (silent if token healthy)
bash scripts/check-claude-oauth.sh
# Force trigger by setting high threshold
WARN_HOURS=24 bash scripts/check-claude-oauth.sh
Configuration
Environment Variable
Default
Description
WARN_HOURS
6
Hours before expiry to start renewal attempts
Troubleshooting
"无法读取 Claude Code token"
- Run
claude auth login manually to establish initial credentials
- Verify keychain access:
security find-generic-password -s "Claude Code-credentials" -a "$(whoami)" -g
Tier 2 (browser automation) not working
- Enable Chrome JXA:
View → Developer → Allow JavaScript from Apple Events
- Or via CLI:
defaults write com.google.Chrome AppleScriptEnabled -bool true (restart Chrome)
- Ensure you're logged into claude.ai in Chrome
JSON parsing errors
- The script uses regex extraction (not
json.loads) to handle truncated keychain output
- If
security -w truncates long values, the -g flag is used as fallback
Notes
- Tier 1 (refresh token) handles most cases silently
- Tier 2 (browser) is only needed when refresh token itself expires (typically weeks)
- Tier 3 (alert) is the last resort when no automated renewal is possible
- The script never stores or logs actual token values
# Normal check (silent if token healthy)
bash scripts/check-claude-oauth.sh
# Force trigger by setting high threshold
WARN_HOURS=24 bash scripts/check-claude-oauth.sh
| Environment Variable | Default | Description |
|---|---|---|
WARN_HOURS |
6 |
Hours before expiry to start renewal attempts |
Troubleshooting
"无法读取 Claude Code token"
- Run
claude auth loginmanually to establish initial credentials - Verify keychain access:
security find-generic-password -s "Claude Code-credentials" -a "$(whoami)" -g
Tier 2 (browser automation) not working
View → Developer → Allow JavaScript from Apple Eventsdefaults write com.google.Chrome AppleScriptEnabled -bool true (restart Chrome)- The script uses regex extraction (not
json.loads) to handle truncated keychain output - If
security -wtruncates long values, the-gflag is used as fallback
Notes
- Tier 1 (refresh token) handles most cases silently
- Tier 2 (browser) is only needed when refresh token itself expires (typically weeks)
- Tier 3 (alert) is the last resort when no automated renewal is possible
- The script never stores or logs actual token values
正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。