Skip to content

接口鉴权 ​

接口说明 ​

系统接口调用时的鉴权。

请求示例 ​

bash
curl --location --request POST '/call/api/v1/dialOut' \
--header 'appid: 1001' \
--header 'nonce: 325476' \
--header 'timestamp: 200000' \
--header 'signature: h+KQeFFlH450COehFWfFlBt111nqiB87e7y5bho1RKVUWI=' \
--header 'content-type: application/json;charset=utf-8' \
--header 'Accept: \*/\*' \
--header 'Connection: keep-alive' \
--data-raw '{
    "agentNumber": "2000",
    "customerNumber": "0100000000",
    "calloutType": "PSTN",
    "agentTimeout": 30,
    "customerTimeout": 60,
    "agentDisNumber": "01012345678",
    "customerDisNumber": "01087654321",
    "extras": {
        "a1": "b1"
    }
}'
名称值说明
content-typeapplication/json;charset=utf-8类型
appid1001账户编号
nonce3254766位随机数用于防止重复请求
timestamp200000请求与服务端时间差大于5分钟会拒绝请求,请确认时间戳的时效。Unix时间戳(秒)。
signatureh+KQeFFlH450COehFWfFlBt111nqiB87e7y5bho1RKVUWI=签名:生成规则如下 1:首先获取secretkey账户秘钥和appid。(开户单鉴权信息一栏内)2:签名加密key = hmac_sha256(appid+timestamp+nonce, secretkey)signature = base64(key)

生成签名代码示例(请自行替换appId和秘钥) ​

Java ​

java
public static void main(String[] args) {
    // appId
    String appId = "1002";
    // 十位秒级时间戳
    Long timestamp = System.currentTimeMillis() / 1000;
    // 6位随机数
    Random random = new Random();
    int nonce = 100000 + random.nextInt(900000);
    // 账号对接秘钥
    String secretKey = "08EB0E95E91D443CA8B1BA3FB930A321";
    try {
        SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256");
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(secretKeySpec);
        String data = appId + timestamp + nonce;
        byte[] hmacBytes = mac.doFinal(data.getBytes());
        String signature = Base64.getEncoder().encodeToString(hmacBytes);
        System.out.println(appId + "  " + timestamp +  " " + nonce + " " + signature);
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
        throw new RuntimeException("Failed to calculate HMAC-SHA256", e);
    }
}

Python ​

python
import hmac
import base64
import random
import time

# appId
app_id = "1002"
# 十位秒级时间戳
timestamp = int(time.time())
# 6位随机数
nonce = str(random.randint(100000, 999999))
# 账号对接秘钥
secret_key = "08EB0E95E91D443CA8B1BA3FB930A321".encode('utf-8')
# 拼接要签名的数据
data = (app_id + str(timestamp) + nonce).encode('utf-8')
# 初始化HMAC-SHA256
hmac_obj = hmac.new(secret_key, digestmod='sha256')
# 计算HMAC-SHA256
hmac_obj.update(data)
hmac_bytes = hmac_obj.digest()
# Base64编码
signature = base64.b64encode(hmac_bytes).decode('utf-8')
# 输出结果
print(app_id, timestamp, nonce, signature)

Go ​

go
package main

import (
	"crypto/hmac"
	"crypto/sha256"
	"encoding/base64"
	"fmt"
	"math/rand"
	"strconv"
	"time"
)

func main() {
	// appId
	appId := "1002"
	// 十位秒级时间戳
	timestamp := strconv.FormatInt(time.Now().Unix(), 10)
	// 6位随机数
	rand.Seed(time.Now().UnixNano())
	nonce := strconv.Itoa(rand.Intn(900000) + 100000)
	// 账号对接秘钥
	secretKey := "08EB0E95E91D443CA8B1BA3FB930A321"
	// 拼接要签名的数据
	data :=  appId + timestamp + nonce
	// 初始化HMAC-SHA256
	h := hmac.New(sha256.New, []byte(secretKey))
	// 计算HMAC-SHA256
	h.Write([]byte(data))
	hmacBytes := h.Sum(nil)
	// Base64编码
	signature := base64.StdEncoding.EncodeToString(hmacBytes)
	// 输出结果
	fmt.Println(appId, timestamp, nonce, signature)
}

Node.js ​

javascript
const crypto = require('crypto');
const randomBytes = require('crypto').randomBytes;

// appId
const appId = '1002';
// 十位秒级时间戳
const timestamp = Math.floor(Date.now() / 1000);
// 6位随机数
const nonce = Math.floor(Math.random() * 900000) + 100000;
// 账号对接秘钥
const secretKey = '08EB0E95E91D443CA8B1BA3FB930A321';
// 拼接要签名的数据
const data = appId + timestamp + nonce;
// 初始化HMAC-SHA256
const hmac = crypto.createHmac('sha256', secretKey);
// 计算HMAC-SHA256
hmac.update(data);
const hmacBytes = hmac.digest();
// Base64编码
const signature = hmacBytes.toString('base64');
// 输出结果
console.log(appId, timestamp, nonce, signature);

PHP ​

php
<?php
// appId
$appId = '1002';
// 十位秒级时间戳
$timestamp = time();
// 6位随机数
$nonce = mt_rand(100000, 999999); // 使用 mt_rand 生成六位随机数
// 账号对接秘钥
$secretKey = '08EB0E95E91D443CA8B1BA3FB930A321';
// 拼接要签名的数据
$data = $appId . $timestamp . $nonce;
// 初始化HMAC-SHA256
$hmac = hash_hmac('sha256', $data, $secretKey, true);
// Base64编码
$signature = base64_encode($hmac);
// 输出结果
echo $appId . ', ' . $timestamp . ', ' . $nonce . ', ' . $signature . PHP_EOL;
?>

C# ​

csharp
using System;
using System.Security.Cryptography;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        // appId
        string appId = "1002";
        // 十位秒级时间戳
        string timestamp = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
        // 6位随机数
        Random rand = new Random();
        string nonce = rand.Next(0, 1000000).ToString("D6");
        // 账号对接秘钥
        string secretKey = "08EB0E95E91D443CA8B1BA3FB930A321";
        // 拼接要签名的数据
        string data = appId + timestamp + nonce;
        // 初始化HMAC-SHA256
        using (HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secretKey)))
        {
            // 计算HMAC-SHA256
            byte[] hmacBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(data));
            // Base64编码
            string signature = Convert.ToBase64String(hmacBytes);
            // 输出结果
            Console.WriteLine($"{appId} {timestamp} {nonce} {signature}");
        }
    }
}