认证机制
OpenHuman API 支持两种认证方式:API 密钥和 OAuth 2.0。
API 密钥
获取 API 密钥
- 登录 OpenHuman Console
- 进入 Settings → API Keys
- 点击 Create New Key
- 复制密钥并妥善保管
使用 API 密钥
curl -H "Authorization: Bearer sk_xxx" \
-H "Content-Type: application/json" \
https://api.tinyhumans.ai/v1/agents
const client = new OpenHuman({
apiKey: 'sk_xxx',
});
密钥安全
- 不要将 API 密钥提交到代码仓库
- 使用环境变量存储密钥
- 定期轮换密钥
- 使用密钥前缀
sk_识别类型
OAuth 2.0
授权流程
用户 → 应用 → OpenHuman → 用户授权 → 获取 Token → API 调用
步骤 1:注册应用
- 进入 Developer Console
- 创建新应用
- 获取
client_id和client_secret
步骤 2:获取授权码
https://auth.tinyhumans.ai/oauth/authorize?
client_id=YOUR_CLIENT_ID&
redirect_uri=YOUR_REDIRECT_URI&
response_type=code&
scope=read write
步骤 3:交换访问令牌
curl -X POST https://auth.tinyhumans.ai/oauth/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=authorization_code" \
-d "code=AUTHORIZATION_CODE" \
-d "client_id=YOUR_CLIENT_ID" \
-d "client_secret=YOUR_CLIENT_SECRET" \
-d "redirect_uri=YOUR_REDIRECT_URI"
响应:
{
"access_token": "at_xxx",
"refresh_token": "rt_xxx",
"expires_in": 3600,
"token_type": "Bearer"
}
步骤 4:使用访问令牌
curl -H "Authorization: Bearer at_xxx" \
https://api.tinyhumans.ai/v1/agents
刷新令牌
访问令牌过期后,使用刷新令牌获取新的访问令牌:
curl -X POST https://auth.tinyhumans.ai/oauth/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=refresh_token" \
-d "refresh_token=rt_xxx" \
-d "client_id=YOUR_CLIENT_ID" \
-d "client_secret=YOUR_CLIENT_SECRET"
scopes 权限范围
| scope | 说明 |
|---|---|
read | 读取资源 |
write | 创建和修改资源 |
delete | 删除资源 |
admin | 管理功能 |
最佳实践
- 优先使用 API 密钥 - 简单场景下更方便
- OAuth 用于需要用户授权的场景 - 如连接第三方服务
- 始终使用 HTTPS - 禁止在 HTTP 下传输密钥
- 最小权限原则 - 只申请需要的 scopes