一、接口鉴权说明
说明:接口调用需要用到统一鉴权、用于确认账户信息和请求的合理性请求路径:https://openapi.useasy.cn鉴权方式:
字段名称 | 字段值 | 字段描述 |
content-type | application/json;charset=utf-8 | 请求类型 |
appid | 1000 | 账户的编号,登录系统后在右上角个人设置查看。 |
nonce | 233334 | 6位随机数用于防止请求重放 |
timestamp | 1687683368 | Unix时间戳(秒),请求与服务端时间差大于5分钟会拒绝请求,请确认时间戳的时效。 |
signature | BGPWsbqqdVJF59aZdFXVw1f9L4+FFilVYJPADuNL10M= | 签名:生成规则如下 1:首先获取secretkey账户秘钥和appid。(开户单鉴权信息一栏内)2:签名加密key = hmac_sha256(appid+timestamp+nonce, secretkey)signature = base64(key) |
请求示例:
curl --location --request POST '/openapi/call/api/v1/callout' \
--header 'appid: 1011' \
--header 'nonce: 123336' \
--header 'timestamp: 123123\
--header 'signature: h+KQeF0CFlH455B87e7ybhO111nqioehFWfFlBt1RKVUWI=' \
--header 'User-Agent: Apifox/1.0.0 (https://www.apifox.cn)' \
--header 'content-type: application/json;charset=utf-8' \
--header 'Accept: */*' \
--header 'Host: 47.99.32.215:8081' \
--header 'Connection: keep-alive' \
--data-raw '{
"agentNumber": "2000",
"customerNumber": "1861111111",
"calloutType": "PSTN",
"agentTimeout": "60",
"customerTimeout": "120",
"agentDisNumber": "01011111111",
"customerDisNumber": "0101111111",
"extras": {
"test": "自定义传参"
}
}'
二、接口请求demo
1、java版本
import com.alibaba.fastjson.JSON;
import org.springframework.http.;
import org.springframework.web.client.RestTemplate;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
*import java.util.*;
public class OpenapiDemo {
public static void main(String[] args) {
String url = "https://openapi.useasy.cn/openapi/platform/v1/agent/list";
String appId = "your_app_id"; // 替换为实际的appId
String secretKey = "your_secret_key"; // 替换为实际的密钥
String nonce = generateRandomSixDigitNumber(); // 6位随机数
long timestamp = System.currentTimeMillis() / 1000; // Unix 时间戳
// 生成签名
String signature = generateSignature(appId, secretKey, nonce, timestamp);
System.out.println("Generated Signature: " + signature);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("appid", appId);
headers.set("nonce", nonce);
headers.set("timestamp", String.valueOf(timestamp));
headers.set("signature", signature);
headers.setContentType(MediaType.APPLICATION_JSON);
// 组装参数
Map<String,String> params = new HashMap<>();
params.put("agentNumber","1234");
String jsonBody = JSON.toJSONString(params);
HttpEntity<String> requestEntity = new HttpEntity(jsonBody, headers);
// 发送请求
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
// 输出响应结果
System.out.println(responseEntity.getBody());
}
private static String generateSignature(String appId, String secretKey, String nonce, long timestamp) {
String message = appId + timestamp + nonce;
Mac mac = null;
try {
mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secretKey.getBytes(), "HmacSHA256"));
// hmac256加密,得到二进制的加密结果
byte[] bytes = mac.doFinal(message.getBytes());
// 对二进制密文进行base64,得到sign
return Base64.getEncoder().encodeToString(bytes);
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
System.out.println(e);
}
return null;
}
private static String generateRandomSixDigitNumber() {
int randomNumber = 100000 + new Random().nextInt(900000); // 生成一个范围在 100000 到 999999 之间的随机数
return String.valueOf(randomNumber);
}
}
2、JavaScript版本
以外呼接口为例
<!DOCTYPE html>
<html>
<script src="./js/hmacsha256.js"></script>
<script src="./js/base64.js"></script>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div>
<a>外呼接口</a>
座席工号<input id="agentNumber" type="text" name="agentNumber" value="8080"/>
被叫号码<input id="customerNumber" type="text" name="customerNumber" value=""/>
账户编号<input id="appid" type="text" value=""/>
密钥<input id="secretKey" type="text" name="secretKey" value="7C537FBC96BA4E35AA1D76CB1DE23F38"/>
<button onclick="callout()">外呼</button>
</div>
</body>
<script type="text/javascript">
function callout(){
//获取输入的参数
var agentNumber = document.getElementById("agentNumber").value;
var customerNumber = document.getElementById("customerNumber").value;
var secretKey = document.getElementById("secretKey").value;
//获取时间戳
var timestamp = Math.round(new Date() / 1000)
//获取随机数
var nonce = randomNum(6);
var appId = document.getElementById("appid").value;
//使用密钥进行hmacsha256加密
var signaturekey = hex(sign(secretKey, appId+timestamp+nonce));
//执行base64 encode
var signature = BASE64.encode(signaturekey);
//判断被叫号码是否填写
if (customerNumber.replace(/(^\s*)|(\s*$)/g, '').length <= 0) {
alert("被叫号码为空")
}else{
requestpost(agentNumber,customerNumber,timestamp,nonce,appId,signature);
}
}
/* 外呼接口 */
function requestpost(agentNumber,customerNumber,timestamp,nonce,appId,signature){
//var url = "https://dev1-opengateway.useasy.cn/openapi/call/api/v1/callout";
//发起post请求
var url = "https://openapi.useasy.cn/openapi/call/api/v1/callout";
var httpRequest = new XMLHttpRequest();
httpRequest.open('POST', url, true);
httpRequest.setRequestHeader("Content-type", "application/json");
httpRequest.setRequestHeader("appid", appId);
httpRequest.setRequestHeader("timestamp", timestamp);
httpRequest.setRequestHeader("nonce", nonce);
httpRequest.setRequestHeader("signature", signature);
httpRequest.setRequestHeader('Access-Control-Allow-Origin', '*');
var obj = {
"agentNumber":agentNumber,
"customerNumber":customerNumber,
"calloutType":"PSTN",
"agentTimeout":"60",
"customerTimeout":"120"
};
httpRequest.send(JSON.stringify(obj));
// 响应后的回调函数
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4 && httpRequest.status == 200) {
var json = httpRequest.responseText;
console.log(json);
}
};
}
function randomNum(n){
var res = "";
for(var i=0;i<n;i++){
res += Math.floor(Math.random()*10);
}
return res;
}
</script>
</html>