tiktok 官方文档:TikTok Shop Partner Center
因为tiktok官方文档生成signature只有go语言版本的
所以记录一下java版本生成tiktok shop 请求open api时需要的signature
源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| private String getSignature(String uri, Map<String, String> parametersMap, String appSecret) throws Exception { Map<String, String> tempParamsMap = new TreeMap<>(Comparator.naturalOrder()); tempParamsMap.putAll(parametersMap); tempParamsMap.remove("sign"); tempParamsMap.remove("access_token"); StringBuilder input = new StringBuilder(uri); for (Map.Entry<String, String> entry : tempParamsMap.entrySet()) { input.append(entry.getKey()).append(entry.getValue()); } input = new StringBuilder(appSecret + input + appSecret); return hmacSHA256(appSecret, input.toString()); }
public static String hmacSHA256(String secret, String message) throws Exception { Mac hmacSha256 = Mac.getInstance("HmacSHA256"); SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256"); hmacSha256.init(secret_key); byte[] bytes = hmacSha256.doFinal(message.getBytes()); StringBuilder hs = new StringBuilder(); String stmp; for (int n = 0; bytes != null && n < bytes.length; n++) { stmp = Integer.toHexString(bytes[n] & 0XFF); if (stmp.length() == 1) hs.append('0'); hs.append(stmp); } return hs.toString().toLowerCase(); }
|
调用方法
原来的url参数加上app_key和timestamp即可