API开发接入

阅读时间约 2 分钟

接口地址

公共参数

参数名称是否必须示例备注
app_key2312521媒体应用 AppKey 查看获取方式
timestamp1596592757秒级时间戳
servicecps-mesh.cpslink.links.post接口服务名称
sign5D17CCCD5B29F98171D6D0D43136B366签名

签名过程

下面以推广转链做调用过程示例

参数示例

//公共参数
app_key=test&timestamp=1596699247&service=cps-mesh.cpslink.links.post
//api参数
{"url":"https://www.jd.com","site_id":"10000"}

1、将请求的公共参数按字典序排序后 key+value 拼接

app_keytestservicecps-mesh.cpslink.links.posttimestamp1596699247

2、尾部拼接 post 请求 json 参数

app_keytestservicecps-mesh.cpslink.links.posttimestamp1596699247{"url":"https://www.jd.com","site_id":"27"}

3、两边拼接 app_secret=(媒体的推送 hash) 如何获取 AppSecret

app_secretapp_keytestservicecps-mesh.cpslink.links.posttimestamp1596699247app_secret

4、对签名字符串 MD5 后转大写得到 sign 签名参数

1326B1AA19903FC2DBCB9AF8CB365A10

5、将公共参数作为 Query 参数拼接得到请求 url

https://open.duomai.com/apis?app_key=test×tamp=1596699247&service=cps-mesh.cpslink.links.post&sign=1326B1AA19903FC2DBCB9AF8CB365A10

6、设置 HTTP 请求头信息,将 API 参数作为 JSON BODY

POST /apis?app_key=test&timestamp=1596699247&service=cps-mesh.cpslink.links.post&sign=1326B1AA19903FC2DBCB9AF8CB365A10 HTTP/1.1
Host: open.duomai.com
Content-Type: application/json

{"url":"https://www.jd.com","site_id":"27"}

代码示例

php

   $appKey = "your siteId";
   $appSecret = "your hash";
   $header = [
       "Content-Type" => "application/json"
   ];
   $query = [
       "app_key" => $appKey,
       "timestamp" => time(),
       "service"=> "cps-mesh.cpslink.links.post"
   ];
   ksort($query);
   $signStr = '';
   foreach ($query as $kev => $val) {
       $signStr .= $kev . $val;
   }
   $body = json_encode([
       "url"=>"https://www.jd.com",
       "site_id"=>"27",
   ]);
   $query["sign"] = strtoupper(md5($appSecret . $signStr . $body . $appSecret));
   try {
       $client = new \GuzzleHttp\Client();
       $response = $client->request('POST', "https://open.duomai.com/apis", [
           "verify"=>false,
           "headers" => $header,
           "query" => $query,
           "body"=> $body
       ]);
   } catch (ClientException $e) {
       $response = $e->getResponse();
   }
   echo $response->getStatusCode();
   echo $response->getHeaderLine('content-type');
   echo $response->getBody()->getContents();die;

golang

    AppKey := "test"
    AppSecret := "test"
    Router:= "https://open.duomai.com/apis/"
    Service:="cps-mesh.cpslink.links.post"
    var body []byte
    params := map[string]string{
        "url":"https://www.jd.com",
        "site_id":"27",
    }
    body,_ = json.Marshal(params)

    nowTime := time.Now().Local()
    // 设置get参数
    val := url.Values{}
    val.Set("timestamp", fmt.Sprintf("%d", nowTime.Unix()))
    val.Set("app_key", AppKey)
    val.Set("service", Service)
    var buf strings.Builder
    keys := make([]string, 0, len(val))
    for k := range val {
        keys = append(keys, k)
    }
    sort.Strings(keys)
    for _, k := range keys {
        vs := val[k]
        keyEscaped := url.QueryEscape(k)
        for _, v := range vs {
            buf.WriteString(keyEscaped)
            buf.WriteString(url.QueryEscape(v))
        }
    }
    // 签名string
    signStr := fmt.Sprintf("%s%s%s%s", AppSecret, buf.String(), string(body), AppSecret)
    // 设置签名
    hash := md5.New()
    hash.Write([]byte(signStr))
    val.Set("sign", strings.ToUpper(fmt.Sprintf("%x", hash.Sum(nil))))
    // 请求
    reqest, _ := http.NewRequest("POST", fmt.Sprintf("%s%s?%s", Router, Service, val.Encode()), bytes.NewBuffer(body))
    reqest.Header.Add("Content-Type", "application/json")
    httpClient := &http.Client{}
    var response *http.Response
    response, err := httpClient.Do(reqest)
    if err != nil {
        return
    }
    defer response.Body.Close()
    byteData, err := ioutil.ReadAll(response.Body)