快速接入API
为方便合作方开发者快速接入API,在此以商品抠图API为例(java),其他后端开发语言可参照该例子进行转化。
前期准备
AK、SK、appId(获取渠道:入驻指引)
AK:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SK:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
appId:xxxxxxxxxxxx
原图url:需处理的原图url
(如出现跨域,需在合作方服务器白名单加上稿定域名:https://open-api.gaoding.com)
接口地址:https://open-api.gaoding.com/api/call/mattingproduct --POST
接口请求路径uri:/api/call/mattingproduct/
接入代码
合作方开发者自己的后端,其他API接口类似
TODO一、二、三、四、五需替换成自己AK、SK、请求路径、原图url等body参数、接口地址、app_id
<code>private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
@ApiOperation(value = "商品抠图", httpMethod = "GET")
@RequestMapping(value = "fetchCommodityKoutu", method = RequestMethod.GET)
public String fetchCommodityKoutu() throws Exception {
// TODO 一、替换成自己AK、SK
String ak = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String sk = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String httpMethod = "POST";
//TODO 二、替换接口请求的路径
String uri = "/api/call/mattingproduct/";
String queryString = "";
JSONObject jsonBody = new JSONObject();
String result = "";
Long timestamp = System.currentTimeMillis() / 1000;
//TODO 三、替换需要处理的原图url等body参数,可参照接口具体body参数
jsonBody.put("url", "https://xxxxxxxxxxxxxxxxxxxxxxxxxx.jpg");
String requestRaw = StringUtils.join(new String[]{httpMethod, "@", uri, "@", queryString, "@", timestamp.toString(), "@", jsonBody.toJSONString()}, "");
String signature = genHmac(requestRaw, sk);
CloseableHttpClient client = HttpClientBuilder.create().build();
//TODO 四、替换接口地址
HttpPost httpPost = new HttpPost("https://open-api.gaoding.com/api/call/mattingproduct");
CloseableHttpResponse response = null;
try {
httpPost.addHeader("Content-Type", "application/json");
httpPost.addHeader("X-Timestamp", timestamp.toString());
httpPost.addHeader("X-AccessKey", ak);
httpPost.addHeader("X-Signature", signature);
//TODO 五、替换成自己app_id
httpPost.addHeader("app_id", "xxxxxxxxxxxx");
httpPost.setEntity(new StringEntity(jsonBody.toString(), Charset.forName("UTF-8")));
response = client.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
System.out.println("响应状态为:" + response.getStatusLine());
if (responseEntity != null) {
result = EntityUtils.toString(responseEntity);
}
} finally {
try {
// 释放资源
if (client != null) {
client.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 使用 HMAC-SHA1 签名方法对data进行签名
*
* @param data 被签名的字符串
* @param key 密钥
* @return 加密后的字符串
*/
//HMAC_SHA1_ALGORITHM="HmacSHA1"
private String genHmac(String data, String key) {
try {
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), HMAC_SHA1_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
return new String(Base64.getEncoder().encode(mac.doFinal(data.getBytes(StandardCharsets.UTF_8))));
} catch (Exception e) {
}
return null;
}
</code>
至此,合作方开发者已经完成商品抠图API的接入和使用啦~
想要了解商品抠图API更多细节,可移步到 商品抠图API