Claude Code Hub 的所有 API 端点通过 HTTP Cookie 进行认证,Cookie 名称为 auth-token。
这是最简单的认证方式,适合在浏览器中测试 API。
步骤:
http://localhost:23000 或您部署的域名)auth-token Cookie(有效期 7 天)优点:
如果需要在脚本、自动化工具或编程环境中调用 API,需要手动获取并设置 Cookie。
步骤:
Application → CookiesStorage → CookiesStorage → Cookiesauth-tokencch_1234567890abcdef...)优点:
# 基本用法:通过 Cookie Header 认证
curl -X POST 'http://localhost:23000/api/actions/users/getUsers' \
-H 'Content-Type: application/json' \
-H 'Cookie: auth-token=your-token-here' \
-d '{}'
# 使用 -b 参数(curl 的 Cookie 简写)
curl -X POST 'http://localhost:23000/api/actions/users/getUsers' \
-H 'Content-Type: application/json' \
-b 'auth-token=your-token-here' \
-d '{}'
# 从文件读取 Cookie
curl -X POST 'http://localhost:23000/api/actions/users/getUsers' \
-H 'Content-Type: application/json' \
-b cookies.txt \
-d '{}'
// Cookie 自动携带,无需手动设置
fetch('/api/actions/users/getUsers', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
credentials: 'include', // 重要:告诉浏览器携带 Cookie
body: JSON.stringify({}),
})
.then(res => res.json())
.then(data => {
if (data.ok) {
console.log('成功:', data.data);
} else {
console.error('失败:', data.error);
}
});
const fetch = require('node-fetch');
// 手动设置 Cookie
fetch('http://localhost:23000/api/actions/users/getUsers', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Cookie': 'auth-token=your-token-here',
},
body: JSON.stringify({}),
})
.then(res => res.json())
.then(data => {
if (data.ok) {
console.log('成功:', data.data);
} else {
console.error('失败:', data.error);
}
});
import requests
# 方式 1:使用 Session(推荐,自动管理 Cookie)
session = requests.Session()
session.cookies.set('auth-token', 'your-token-here')
response = session.post(
'http://localhost:23000/api/actions/users/getUsers',
json={},
)
if response.json()['ok']:
print('成功:', response.json()['data'])
else:
print('失败:', response.json()['error'])
# 方式 2:直接在 headers 中设置 Cookie
response = requests.post(
'http://localhost:23000/api/actions/users/getUsers',
json={},
headers={
'Content-Type': 'application/json',
'Cookie': 'auth-token=your-token-here'
}
)
import httpx
async def get_users():
async with httpx.AsyncClient() as client:
response = await client.post(
'http://localhost:23000/api/actions/users/getUsers',
json={},
headers={
'Cookie': 'auth-token=your-token-here'
}
)
return response.json()
# 使用示例
import asyncio
result = asyncio.run(get_users())
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
func main() {
url := "http://localhost:23000/api/actions/users/getUsers"
// 创建请求体
body := bytes.NewBuffer([]byte("{}"))
// 创建请求
req, err := http.NewRequest("POST", url, body)
if err != nil {
panic(err)
}
// 设置 Headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Cookie", "auth-token=your-token-here")
// 发送请求
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// 解析响应
respBody, _ := io.ReadAll(resp.Body)
var result map[string]interface{}
json.Unmarshal(respBody, &result)
if result["ok"].(bool) {
fmt.Println("成功:", result["data"])
} else {
fmt.Println("失败:", result["error"])
}
}
原因: 缺少 auth-token Cookie
解决方法:
Cookie: auth-token=... Headercredentials: 'include'原因: Cookie 无效、已过期或已被撤销
解决方法:
auth-tokencanLoginWebUi 权限原因: 当前用户没有访问该端点的权限
解决方法:
[管理员])ADMIN_TOKEN 或具有 admin 角色的用户)原因: 未设置 credentials: 'include'
解决方法:
fetch('/api/actions/users/getUsers', {
credentials: 'include', // 添加这一行
// ... 其他配置
})
原因: CORS 策略限制
解决方法:
credentials: 'include'不要在公共场合分享 Cookie 值
auth-token 相当于您的登录凭证定期更换 API Key
使用 HTTPS
ENABLE_SECURE_COOKIES=true(默认值)环境变量管理