身份验证
鉴权依据
API ID、API Secret
注:由于数据敏感,所以只在主账号中可见,具体展示位置为:
“管理控制台”--〉“采集信息”
URL参数
http://{yourDomain}/api/{service}?api_id=abcdef&api_sign=abcdef&api_ts=123456
参数名 | 描述 | 备注 |
api_id | API ID | Quick Tracking后台主账号可查看 |
api_sign | 签名 | 具体逻辑见“鉴权签名” |
api_ts | 时间戳 | 毫秒 |
body参数
以下图中的维度表为例:
{
"dimId": "24932301303965",
"dimensionValueParamList": [
{
"idValue": "huawei",
"level1": "华为"
"level2": "华为P60"
},
{
"idValue": "xiaomi",
"level1": "小米"
"level2": "小米10"
}
]
}
字段名称 | 字段含义 | 是否必填 | 备注 |
dimId | 维度表id | 必填 | |
dimensionValueParamList | 需要添加后修改的纬度值 | 必填 | |
idValue | ID值 | 必填 | |
level1 | 层级1 | 必填 | 至少要有一个层级 |
level2~n | 层级2~n | 非必填 |
注:
上传的idValue已在维度表中存在,为覆盖已经存在的维度值;上传的idValue不在维度表中存在,则为追加新的维度值。
目前dimId获取方式如下
鉴权签名
方法名 + 参数按照key进行字母排序
/**
* Quick Tracking后台主账号可查看
*/
String secret = "abcdef";
/**
* 当前要请求的服务名
*/
String service = "general.dimensionValue.addOrModify";
/**
* 1.获取url参数,如apiId=abcdef&sign=abcdef&ts=123456
* 2.按照key排序,移除sign,结果apiId=abcdef&ts=123456
*/
String queryString = sort("api_id=abcdef&api_ts=123456");
/**
* POST接口,body内容
*/
String bodyString = "{
{
"dimId": "24932301303965",
"dimensionValueParamList": [
{
"idValue": "A001",
"level1": "1"
},
{
"idValue": "A002",
"level1": "2"
}
]
}";
String source = service
+ "\n"
+ queryString
+ "\n"
+ bodyString;
用API Secret对source进行加密
String sign = new HmacUtils(HmacAlgorithms.HMAC_SHA_1, secret).hmacHex(source);
sign = 1cfc10a297397e91f1e50e1f41ac24b8c45fd53d
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.3.0</version>
</dependency>package com.alibaba.umeng.base.product.advanced.controller;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import org.apache.commons.codec.digest.HmacAlgorithms;
import org.apache.commons.codec.digest.HmacUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
/**
* @author mingpeng.spc
* @date 2022/01/07
*/
public class OpenApiControllerTest {
private static final MediaType APPLICATION_JSON = MediaType.parse("application/json; charset=utf-8");
private final static Logger LOGGER = LoggerFactory.getLogger(OpenApiControllerTest.class);
public static void main(String[] args) {
String url = "http://pre.aplus.emas-poc.com/api/";
String service = "general.dimensionValue.addOrModify";
String apiId = "avWYU24hvdl62V8p";
String apiSecret = "xezIgkcrtZ2yLQA4LYgBuyUo6Re2hCu8";
/**
* 构造url参数
*/
String query = new StringBuilder()
.append("api_id=").append(apiId)
.append("&")
.append("api_ts=").append(System.currentTimeMillis())
.toString();
/**
* 构造body参数
*/
JSONObject body = new JSONObject();
body.put("dimId", "24932301303965");
body.put("dimensionValueList", "xxx");
String bodyString = body.toJSONString();
/**
* service
* query
* body
*/
StringBuilder valueToDigest = new StringBuilder()
.append(service)
.append("\n")
.append(query)
.append("\n")
.append(bodyString);
String sign = new HmacUtils(HmacAlgorithms.HMAC_SHA_1, apiSecret).hmacHex(valueToDigest.toString());
try {
Response response = post(url + service + "?" + query + "&api_sign=" + sign, bodyString);
System.out.println(response.body().string());
} catch (Exception e) {
LOGGER.info("invoke post error", e);
}
}
/**
* HTTP Post
* @param url URL
* @param body 请求body
* @return
*/
private static Response post(String url, String body) throws IOException {
LOGGER.info("http post start, url = {}, body = {}", url, body);
RequestBody requestBody = RequestBody.create(APPLICATION_JSON, body);
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Response response = new OkHttpClient().newCall(request).execute();
LOGGER.info("http post success");
return response;
}
}
请求参数构建
{
"dimId": "24932301303965",
"dimensionValueParamList": [
{
"idValue": "A001",
"level1": "1"
},
{
"idValue": "A002",
"level1": "2"
}
]
}
字段名称 | 字段含义 | 是否必填 | 备注 |
dimId | 维度表id | 必填 | |
dimensionValueParamList | 需要添加后修改的纬度值 | 必填 | |
idValue | ID值 | 必填 | |
level1 | 层级1 | 必填 | 至少要有一个层级 |
level2~n | 层级2~n | 非必填 |
返回结果
{
"code": 200,
"msg": "SUCCESS",
"sCode": 200,
"sMsg": "SUCCESS",
"traceId": "1ec5e2df16769551646401019d120f",
"success": true
}
文档内容是否对您有帮助?