更新变量数据接口API
提示:#
- 因为运营商政策,请先在后台完成企业认证再开发 API。
HTTP 头信息(具体参考调用说明):#
Accept:application/json;charset=utf-8; Content-Type:application/json;charset=utf-8; X-SIGNATURE: $signature; X-APIKEY: $APIKEY; X-TIMESTAMP: $timestamp; X-NONCE: $nonce;请求#
URL:`https://gateway.yihuitong.top/callai-openapi/callai/udpateCallData` 访问方式:POST请求参数#
| 参数名 | 类型 | 是否必传 | 是否默认开放 | 描述 | 示例 |
|---|---|---|---|---|---|
| sid | string | 否 | 是 | 呼叫ID 和 busId至少需要提交一个字段 | |
| busId | string | 否 | 是 | 呼叫业务提交ID | |
| customerInfo | map | 是 | 是 | 变量数据 |
代码示例(头信息加密验签参考)#
- Java
- C#
- PHP
/** * 批量提交呼叫数据接口案例 * * @return json格式字符串 * @throws */ public static String cancelCall() throws NoSuchAlgorithmException , InvalidKeyException { String smsUrl = "https://gateway.yihuitong.top/callai-openapi/callai/udpateCallData"; String nonce = "bc9efee185e64ab9bc0b07a2785c4660"; Integer timestamp = 1626856279; String apikey = "123456789"; String secretKey = "1234567890"; String method = "POST"; String path = "/callai-openapi/callai/udpateCallData"; String contentType = ""; Map<String, Object> variable = new HashMap<>(); variable.put("name", "张三1"); Map<String, Object> parameters = new HashMap<>(); parameters.put("busId","12345"); parameters.put("customerInfo", variable); StringBuilder sb = new StringBuilder(); sb.append(method).append("\n") .append(path).append("\n") .append(apikey).append("\n") .append(timestamp).append("\n") .append(nonce).append("\n"); if(parameters!=null && !parameters.isEmpty()) { if("json".equals(contentType) ) { sb.append(JSON.toJSONString(parameters)).append("\n"); }else { String canonical_query_string = formatUrlMap(parameters, true, false); sb.append(canonical_query_string).append("\n"); } }
Mac hasher = Mac.getInstance("HmacSHA256"); hasher.init(new SecretKeySpec(secretKey.getBytes(), "HmacSHA256")); byte[] hash = hasher.doFinal(sb.toString().getBytes()); Map<String,String> headers = new HashMap<String, String>(); headers.put("X-SIGNATURE", DatatypeConverter.printBase64Binary(hash)); headers.put("X-APIKEY", apikey); headers.put("X-TIMESTAMP", timestamp.toString()); headers.put("X-NONCE", nonce); return HttpUtil.get(smsUrl, headers);//请自行使用get方式请求,可使用Apache HttpClient }
public static String formatUrlMap(Map<String, Object> paraMap,boolean urlEncode,boolean keyToLower) { if(CollUtil.isEmpty(paraMap)) { return null; } if(!(paraMap instanceof TreeMap)) { paraMap = new TreeMap<String, Object>(paraMap); } return paraMap.entrySet().stream().map(entry -> { String key = entry.getKey(); Object value = entry.getValue(); if(urlEncode) { try { key = URLEncoder.encode(key, "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if(value!=null && StringUtils.isNotBlank(value.toString())) { try { value = URLEncoder.encode(value.toString(), "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } if (keyToLower) { key = key.toLowerCase(); } } return key + "=" + value; }).collect(Collectors.joining("&")); }
//编译版本 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
using System;using System.Collections.Generic;using System.Linq;using System.Text.RegularExpressions;using Newtonsoft.Json;using System.Security.Cryptography;using System.Net;using System.IO;using System.Text;
namespace YiHuiTong{ public class Signature { public static void Main(string[] args) { string smsUrl = https://gateway.yihuitong.top/callai-openapi/callai/udpateCallData"; string nonce = System.Guid.NewGuid().ToString("N"); string timestamp = time(); string apikey = "123456789"; string secretKey = "1234567890"; string method = "POST"; string path = "/callai-openapi/callai/udpateCallData";
Dictionary<string, string> variable = new Dictionary<string, string>(); variable.Add("name", "张三1"); Dictionary<string, Object> map = new Dictionary<string, Object>(); map.Add("busId","12345"); map.Add("customerInfo", variable); string message = new HmacStringBuild(SigningHeadersContentType.FORM) .setMethod(method) .setPath(path) .setApikey(apikey) .setTimestamp(timestamp) .setNonce(nonce) .setData(map) .build(); Console.WriteLine(message); string SIGNATURE = CreateToken(message,secretKey); Console.WriteLine(SIGNATURE); Dictionary<string, string> headers = new Dictionary<string, string>(); headers.Add("X-SIGNATURE",SIGNATURE); headers.Add("X-TIMESTAMP",timestamp); headers.Add("X-APIKEY",apikey); headers.Add("X-NONCE",nonce); string returnData = Get(smsUrl,headers); Console.WriteLine(returnData); } public static string CreateToken(string message, string secretKey) { secretKey = secretKey ?? ""; var encoding = new System.Text.UTF8Encoding(); byte[] keyByte = encoding.GetBytes(secretKey); byte[] messageBytes = encoding.GetBytes(message); using (var hmacsha256 = new HMACSHA256(keyByte)) { byte[] hashmessage = hmacsha256.ComputeHash(messageBytes); return Convert.ToBase64String(hashmessage); } } public static string time() { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds).ToString(); } public static string formatUrlMap(Dictionary<string, Object> paraMap,bool urlEncode) { string para = ""; foreach (KeyValuePair<string, Object> kvp in paraMap) { string key = kvp.Key; string _value = kvp.Value.ToString(); if(urlEncode) { key = System.Web.HttpUtility.UrlEncode(key); if(_value!=null && _value.Length==0){ _value = System.Web.HttpUtility.UrlEncode(_value); } } para += key + "=" + _value + "&"; } return para.Substring(0,para.Length - 1); } public static string Get(string serviceUrl, Dictionary<string, string> headers) {
//创建Web访问对象 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(serviceUrl); //把用户传过来的数据转成“UTF-8”的字节流 byte[] buf = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(parameterData);
myRequest.Method = "GET"; myRequest.AutomaticDecompression = DecompressionMethods.GZip; myRequest.ContentType = "application/json; charset=UTF-8"; myRequest.ContentLength = buf.Length; myRequest.MaximumAutomaticRedirections = 1; myRequest.AllowAutoRedirect = true;
myRequest.Headers.Add("X-SIGNATURE", headers["X-SIGNATURE"]); myRequest.Headers.Add("X-TIMESTAMP", headers["X-TIMESTAMP"]); myRequest.Headers.Add("X-APIKEY", headers["X-APIKEY"]); myRequest.Headers.Add("X-NONCE", headers["X-NONCE"]);
//发送请求 Stream stream = myRequest.GetRequestStream(); stream.Write(buf, 0, buf.Length); stream.Close();
//通过Web访问对象获取响应内容 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); //通过响应内容流创建StreamReader对象,因为StreamReader更高级更快 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); //string returnXml = HttpUtility.UrlDecode(reader.ReadToEnd());//如果有编码问题就用这个方法 string returnData = reader.ReadToEnd();//利用StreamReader就可以从响应内容从头读到尾
reader.Close(); myResponse.Close();
return returnData; } } class HmacStringBuild { private SigningHeadersContentType contentType; private string method; private string path; private string apikey; private string timestamp; private string nonce; private Dictionary<string, System.Object> data; public HmacStringBuild(SigningHeadersContentType contentType){ this.contentType = contentType; } public HmacStringBuild setMethod(string method){ this.method = method; return this; } public HmacStringBuild setPath(string path){ this.path = path; return this; } public HmacStringBuild setApikey(string apikey){ this.apikey = apikey; return this; } public HmacStringBuild setTimestamp(string timestamp){ this.timestamp = timestamp; return this; } public HmacStringBuild setNonce(string nonce){ this.nonce = nonce; return this; } public HmacStringBuild setData(Dictionary<string, System.Object> data){ this.data = data; return this; } public string build(){ String str = this.method + "\n" + this.path + "\n" + this.apikey + "\n" + this.timestamp + "\n" + this.nonce + "\n"; if(string.Equals("GET",this.method)){ str = str + Signature.formatUrlMap(this.data,true) + "\n"; }else if(contentType == SigningHeadersContentType.JSON){ str = str + JsonConvert.SerializeObject(this.data) + "\n"; }else{ str = str + Signature.formatUrlMap(this.data,false) + "\n"; } return str; } } enum SigningHeadersContentType{ JSON,FORM };}<?php
function uuid() { $chars = md5(uniqid(mt_rand(), true)); $uuid = substr ( $chars, 0, 8 ) . '-' . substr ( $chars, 8, 4 ) . '-' . substr ( $chars, 12, 4 ) . '-' . substr ( $chars, 16, 4 ) . '-' . substr ( $chars, 20, 12 ); return $uuid ; } $smsUrl = "https://gateway.yihuitong.top/callai-openapi/callai/udpateCallData"; $nonce = uuid(); $timestamp = time(); $apikey = "123456789"; $secretKey = "1234567890"; $method = "POST"; $path = "/callai-openapi/callai/udpateCallData"; $customerInfoMap = array(['name': '李四']); $data = json_encode(array(['busId'=> '12345','customerInfo'=> $customerInfoMap)]);
echo $data; $message = $method . "\n" . $path . "\n" . $apikey . "\n" . $timestamp . "\n" . $nonce . "\n" . $data . "\n";
$SIGNATURE = base64_encode(hash_hmac('sha256', $message, $secretKey,true)); // to base64
$header = array( 'X-SIGNATURE: ' . $SIGNATURE, 'X-TIMESTAMP: ' . $timestamp, 'X-APIKEY: ' . $apikey, 'X-NONCE: ' . $nonce, 'Content-Length: ' . strlen($data) );
list($return_code, $return_content) = http_get_data($smsUrl, $header);
echo $return_code; echo $return_content;?>响应参数#
| 名称 | 类型 | 描述 |
|---|---|---|
| success | boolean | 是否请求成功 false 为接口请求失败,请求失败 data 中无数据 |
| errorCode | string | 错误码 200 代表成功 |
| errorMessage | string | 错误提示 |
| data | string | 提示描述 |
JSON 响应示例#
{ "success": true, "errorCode": "200", "errorMessage": "成功", "data": "修改成功"}errorCode 状态码列表#
| errorCode | 描述 |
|---|---|
| 200 | 成功(收费) |
| 4412 | 查无记录(不收费) |
| 4001 | 用户信息不正确 |
| 4100 | 参数错误 |
| 4300 | 金额不足 |
| 4301 | 账号未认证,请先认证通过后访问 |
| 4402 | 提交失败 |
| 4404 | 扣费失败 |
| 5000 | 内部错误 |