身份验证
本文档提供了如何通过天际 API 进行身份验证的详细说明,包括如何获取、使用和管理 API 密钥。
身份验证方法
天际 API 使用 Bearer Token 身份验证。您需要在每次 API 请求的 HTTP 头中包含您的 API 密钥。
HTTP 头格式
Authorization: Bearer <YOUR_API_KEY>
获取 API 密钥
- 登录到您的天际实例
- 点击右上角的头像
- 找到 API 密钥 部分
- 点击 + 按钮创建一个新的 API 密钥
- 为您的 API 密钥命名并保存
API 密钥管理
查看现有密钥
在 API 密钥 部分,您可以看到:
- API 密钥名称/描述
- 创建日期
- 上次使用时间
- 使用次数统计
删除 API 密钥
如需撤销 API 密钥:
- 找到您想要删除的 API 密钥
- 点击 删除 按钮
- 确认删除操作
注意
删除 API 密钥后,所有使用该密钥的应用程序将无法访问 API。
使用 API 密钥
cURL 示例
curl -X GET "https://your-tianji-domain.com/open/global/config" \
-H "Authorization: Bearer <your_api_key_here>" \
-H "Content-Type: application/json"
JavaScript/Node.js 示例
const apiKey = '<your_api_key_here>';
const baseUrl = 'https://your-tianji-domain.com/open';
const headers = {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
};
// 使用 fetch
const response = await fetch(`${baseUrl}/global/config`, {
method: 'GET',
headers: headers
});
// 使用 axios
const axios = require('axios');
const response = await axios.get(`${baseUrl}/global/config`, {
headers: headers
});
Python 示例
import requests
api_key = '<your_api_key_here>'
base_url = 'https://your-tianji-domain.com/open'
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
# 使用 requests 库
response = requests.get(f'{base_url}/global/config', headers=headers)
data = response.json()