跳到主要内容

签名算法(X-Api-Key 生成规则)

所有 OpenAPI 请求都需要在请求头携带以下两项:

X-Api-Account: <act>
X-Api-Key: <timestamp.signature>

生成步骤

  1. 获取当前 Unix 时间戳(毫秒) timestamp = 1718866800000
  2. 构造原文 data = act + ":" + timestamp
  3. 使用 ApiKey 做 HmacSHA256 并 Base64 编码 signature = Base64(HmacSHA256(data, ApiKey))
  4. 最终请求头值 X-Api-Key: timestamp.signature

Java 示例(与源码一致)

String timestamp = String.valueOf(System.currentTimeMillis());
String data = ACT + ":" + timestamp;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(API_KEY.getBytes(), "HmacSHA256"));
String signature = Base64.getEncoder().encodeToString(mac.doFinal(data.getBytes()));
String headerKey = timestamp + "." + signature;

Python 示例

import hmac
import hashlib
import base64
import time

timestamp = str(int(time.time() * 1000))
data = f"{account}:{timestamp}"
signature = base64.b64encode(
hmac.new(api_key.encode(), data.encode(), hashlib.sha256).digest()
).decode()
header_key = f"{timestamp}.{signature}"
时间戳有效期

签名中的时间戳有效期为 3 分钟,请确保服务器时间准确(建议开启 NTP 同步)。签名验证失败时,请优先检查时间戳是否过期、签名算法是否正确。