帮助中心
签名机制

为防止请求参数被恶意篡改,提高接口安全性,调用壹沓OpenAPI需要进行签名。请求接口时,需将签名写入请求头(Header)中,验签通过后才会发起业务请求。

# 前提条件

调用API接口前需要获取接口调用凭证。具体操作,请参见调用凭证 (opens new window)

# 签名生成

# 参数说明

参数名称 描述
accessToken 调用接口的凭证。获取方式,请参见调用凭证 (opens new window)
nonce 随机数,每次请求都不能相同。
timestamp 时间戳,单位毫秒,可以取当前接口调用时间。
secret AccessKey Secret。获取方式,请参见OpenAPI管理 (opens new window)
sign 接口签名。

# 操作步骤

  1. 将accessToken、nonce、timestamp、secret 按照此顺序组合key=value形式,并以 “&” 符号拼接生成字符串。
accessToken=调用接口的凭证&nonce=随机数&timestamp=时间戳&secret=AccessKey Secret
1
  1. 通过MD5加密签名字符串,获取32位小写密文的sign。
sign=MD5(accessToken=调用接口的凭证&nonce=随机数&timestamp=时间戳&secret=AccessKey Secret)
1
  1. 发起请求时,将accessToken、nonce、timestamp、sign填充到HTTP请求头(Header)中。

# Java示例

import cn.hutool.crypto.SecureUtil;
 
import java.util.TreeMap;
import java.util.UUID;

private static void sign() {
        String url = "your url";
        String secret = "your access secret";
        String accessToken = "your access token";

        String nonce = UUID.randomUUID().toString();
        String timestamp = String.valueOf(System.currentTimeMillis());

        TreeMap<String, String> queryParamTreeMap = new TreeMap<>();
        queryParamTreeMap.put("accessToken", accessToken);
        queryParamTreeMap.put("nonce", nonce);
        queryParamTreeMap.put("timestamp", timestamp);

        StringBuilder signStr = new StringBuilder();
        for (String key : queryParamTreeMap.keySet()) {
            signStr.append(key).append("=").append(queryParamTreeMap.get(key)).append("&");
        }
        signStr.append("secret=").append(secret);
        String sign = SecureUtil.md5(signStr.toString());

        System.out.println("accessToken : " + accessToken);
        System.out.println("nonce : " + nonce);
        System.out.println("timestamp : " + timestamp);
        System.out.println("sign : " + sign);
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

# Python示例

import uuid
from hashlib import md5
import time
 
 
def get_sign_func(accessToken, secretKey):
    """
    生成接口签名系统参数
 
    :param accessToken: 调用接口的凭证accessToken <str>
    :param secretKey: Secret Access Key <str>
    :returns 返回值为nonce <str>、timestamp <str>、sign <str>
    """
    nonce = str(uuid.uuid1())
    timestamp = str(int(time.time()*1000))
    sign_str = "accessToken={}&nonce={}&timestamp={}&secret={}".format(accessToken, nonce, timestamp, secretKey)
    sign = md5(sign_str.encode(encoding="utf-8")).hexdigest()
    return nonce, timestamp, sign
 
 
if __name__ == "__main__":
    accessToken = "your access token"
    secretKey = "your secret key"
    nonce, timestamp, sign = get_sign_func(accessToken, secretKey)
    print("nonce : {}".format(nonce))
    print("timestamp : {}".format(timestamp))
    print("sign : {}".format(sign))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
最近更新时间: { "value": "2023-10-09", "effect": true }