文章目录
从零到一:Temu API对接全流程指南
本文详细介绍了Temu API对接的关键步骤,包括加签算法的实现、API权限申请流程、客户端的构建以及代码实现。首先,文章讲解了如何通过与Temu买手联系开通API权限并获取accessToken。随后,详细阐述了如何编写客户端(ApiClient)并实现加签算法以确保请求的安全性。最后,通过具体代码示例展示了如何测试ApiClient并验证请求的正确性,确保API集成顺利完成,最终实现自动化的商品与订单管理。
对接Temu API的前提条件
要对接Temu API,首先需要确保你的店铺符合以下条件:
拥有一个Temu店铺:你必须是Temu平台的卖家。
店铺上新数量超过100件:这是Temu对接API的一个基本要求,确保你的店铺有足够的商品可供调用API。
步骤 1:找Temu买手开通API权限
首先,你需要联系Temu的买手,申请开通API权限。成功开通后,系统会在后台为你生成一个应用,包含两个关键参数:
appKey
appSecret
这两个参数是调用Temu API时必需的身份验证信息。
步骤 2:为应用授权
授权后,你将获得一个重要的参数:accessToken。请注意,accessToken是有有效期的,通常为3个月,所以在3个月内需要进行刷新,过期后无法继续使用。
步骤 3:查看对接文档,完成对接
买手会提供一份API对接文档链接。要查看该文档,你需要使用Temu卖家中心的主账号登录。需要特别注意,文档中的授权步骤也必须登录主账号后才能执行。
步骤 4:编写代码处理请求
Temu API的请求参数分为两部分:
公共参数:这些参数是所有API请求通用的,如appKey、appSecret等。
业务参数:这些参数与具体的API请求内容相关,例如商品信息、订单详情等。
在设置好这些参数后,你还需要进行签名加密(即加签)。加签是确保请求安全和数据完整性的重要步骤。
公共参数类
@Data
public class CommonParams {
/**
* 请求类型,通常用来标识请求的具体类型或操作。
*/
private String type;
/**
* 应用密钥,用于标识调用 API 的应用。
* 应用密钥由 Temu API 提供,用于验证请求来源。
*/
private String appKey;
/**
* 应用密钥密文,通常用于签名生成,确保请求的安全性。
* 该值在调用 API 时通常是私密的,不应公开。
*/
private String appSecret;
/**
* 访问令牌,用于授权访问 Temu API。
* 访问令牌通常是在用户授权后获得,用于表明当前请求属于哪个用户。
*/
private String accessToken;
}
编写ApiClient与实现加签算法
import com.alibaba.fastjson.JSONObject;
import com.sifan.core.dto.CommonParams;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.security.MessageDigest;
import java.time.Instant;
import java.util.*;
/**
* Temu API 请求接口
*
* @author dengzhilin
*/
public class TemuApiClient {
// API请求的URL地址
private static String url;
// 用于发送HTTP请求的RestTemplate对象
private static RestTemplate restTemplate;
// HTTP请求头
private static HttpHeaders httpHeaders;
// 签名的字段名
private static String SIGN = "sign";
// HTTP请求方法(POST)
private static HttpMethod method;
// 静态代码块,在类加载时初始化必要的变量
static {
// 设置CN区API网关的URL
url = "https://openapi.kuajingmaihuo.com/openapi/router";
// 创建RestTemplate实例
restTemplate = new RestTemplate();
// 初始化HTTP请求头并设置Content-Type为application/json
httpHeaders = new HttpHeaders();
httpHeaders.add("content-type", "application/json");
// 设置请求方法为POST
method = HttpMethod.POST;
}
/**
* 发送请求到 Temu API 接口并返回响应数据。
* <p>
* 该方法将公共参数与业务参数组合,生成完整的请求,发送到 Temu API,并解析返回的响应数据。
*
* @param commonParams 包含 Temu API 的公共参数,如应用密钥、访问令牌、时间戳等。
* @param businessParams 包含特定业务操作所需的参数,例如页号、页码等。
* @return 返回一个 JSONObject,包含 Temu API 返回的响应数据。
*/
public static JSONObject sendRequest(CommonParams commonParams, Map<String, Object> businessParams) {
if (businessParams == null) {
businessParams = new HashMap<>(10);
}
// 如果参数中包含签名字段,则移除该字段
if (businessParams.containsKey(SIGN)) {
businessParams.remove(SIGN);
}
// 获取temuPublicParam中的各个参数
String type = commonParams.getType();
String appKey = commonParams.getAppKey();
String appSecret = commonParams.getAppSecret();
String accessToken = commonParams.getAccessToken();
// 将必要的参数加入到paramMap中
businessParams.put("type", type);
businessParams.put("app_key", appKey);
businessParams.put("access_token", accessToken);
businessParams.put("timestamp", Instant.now().getEpochSecond()); // 当前时间戳
// 生成签名
String sign = generateSign(businessParams, appSecret);
// 将生成的签名添加到paramMap中
businessParams.put(SIGN, sign);
// 将参数和请求头封装为HttpEntity
HttpEntity<Map<String, Object>> mapHttpEntity = new HttpEntity<>(businessParams, httpHeaders);
// 发送HTTP请求并获取响应
ResponseEntity<String> response = restTemplate.exchange(url, method, mapHttpEntity, String.class);
// 获取响应体内容
String body = response.getBody();
// 将响应体转换为JSON对象并返回
JSONObject bodyJson = JSONObject.parseObject(body);
return bodyJson;
}
/**
* 生成签名的方法
*
* @param paramMap 请求参数的键值对
* @param appSecret 应用的密钥
* @return 生成的签名字符串
*/
private static String generateSign(Map<String, Object> paramMap, String appSecret) {
// Step 1: 按照键的ASCII码顺序对参数进行排序
List<String> keys = new ArrayList<>(paramMap.keySet());
Collections.sort(keys);
// Step 2: 按照"key + value"的顺序将所有参数拼接成一个字符串
StringBuilder stringBuilder = new StringBuilder();
for (String key : keys) {
Object value = paramMap.get(key);
if (value != null) {
String jsonValue = null;
if (value instanceof String) {
jsonValue = (String) value;
} else {
jsonValue = JSONObject.toJSONString(value);
}
stringBuilder.append(key).append(jsonValue);
}
}
// Step 3: 在拼接好的字符串前后分别添加appSecret
String signString = appSecret + stringBuilder.toString() + appSecret;
// Step 4: 使用MD5算法对字符串进行加密,并将结果转换为大写
return md5(signString).toUpperCase();
}
/**
* 对字符串进行MD5加密
*
* @param input 输入的字符串
* @return MD5加密后的字符串
*/
private static String md5(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] digest = md.digest(input.getBytes("UTF-8"));
StringBuilder hexString = new StringBuilder();
for (byte b : digest) {
String hex = Integer.toHexString(0xFF & b);
if (hex.length() == 1) {
hexString.append('0'); // 补零确保两位十六进制
}
hexString.append(hex);
}
return hexString.toString();
} catch (Exception e) {
throw new RuntimeException("生成MD5哈希失败", e);
}
}
}
测试ApiClient
@Test
public void test01() {
// 测试账号进行测试
String appKey = "xxxxxx自己的appKey xxxxxx";
String appSecret = "xxxxx自己的appSecret xxxxxxx";
String accessToken = "xxxx自己的accessToken xxxxxxxx";
String type = "bg.goods.list.get";
// ------------------------------------------------------------
// 公共参数
CommonParams commonParams = new CommonParams();
commonParams.setType(type);
commonParams.setAppKey(appKey);
commonParams.setAppSecret(appSecret);
commonParams.setAccessToken(accessToken);
// 业务参数
Map<String, Object> businessParams = new HashMap<>(10);
JSONObject bodyJson = TemuApiClient.sendRequest(commonParams, businessParams);
System.out.println(bodyJson);
}
测试结果如下: