Java 获取拼多多商品详情简易版 API 接口实现
拼多多开放平台提供了丰富的 API 接口,其中 pdd.ddk.goods.detail 接口可以用于获取商品的详细信息,包括商品价格、优惠券信息等。以下是一个基于 Java 的简易实现方案,包含签名生成、HTTP 请求及响应解析逻辑。
一、核心依赖与准备工作
(一)依赖引入
需要添加 HTTP 客户端和 JSON 解析依赖(以 Maven 为例):
<dependencies><!-- HTTP客户端 --><dependency><groupId>org.apache.httpcomponents.client5</groupId><artifactId>httpclient5</artifactId><version>5.3</version></dependency><!-- JSON解析 --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>2.0.32</version></dependency>
</dependencies>(二)认证信息
从拼多多开放平台获取 clientId 和 clientSecret,推广位 ID(pid,可选,用于获取专属优惠)。
二、Java 实现代码
以下代码实现了拼多多 API 调用框架,通过 pdd.ddk.goods.detail 接口获取商品到手价。
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;public class PddPriceClient {private static final String API_URL = "https://gw-api.pinduoduo.com/api/router";private static final String CHARSET = "UTF-8";private final String clientId;private final String clientSecret;/*** 初始化客户端** @param clientId 应用 clientId* @param clientSecret 应用 clientSecret*/public PddPriceClient(String clientId, String clientSecret) {this.clientId = clientId;this.clientSecret = clientSecret;}/*** 生成签名** @param params 所有请求参数* @return 签名字符串*/private String generateSign(Map<String, String> params) {try {// 1. 按参数名 ASCII 升序排序List<Map.Entry<String, String>> entryList = new ArrayList<>(params.entrySet());entryList.sort(Comparator.comparing(Map.Entry::getKey));// 2. 拼接为 key=value&key=value 格式StringBuilder signSb = new StringBuilder();for (int i = 0; i < entryList.size(); i++) {Map.Entry<String, String> entry = entryList.get(i);signSb.append(entry.getKey()).append("=").append(entry.getValue());if (i != entryList.size() - 1) {signSb.append("&");}}// 3. 拼接 clientSecret 并 MD5 加密signSb.append(clientSecret);MessageDigest md = MessageDigest.getInstance("MD5");byte[] digest = md.digest(signSb.toString().getBytes(StandardCharsets.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().toUpperCase();} catch (NoSuchAlgorithmException e) {throw new RuntimeException("MD5 algorithm not found", e);}}/*** 获取商品详情** @param goodsId 商品 ID* @return 商品详情 JSON 对象* @throws IOException 网络请求异常* @throws ParseException 响应解析异常*/public JSONObject getGoodsDetail(String goodsId) throws IOException, ParseException {// 构造请求参数Map<String, String> params = new HashMap<>();params.put("type", "pdd.ddk.goods.detail");params.put("client_id", clientId);params.put("timestamp", String.valueOf(System.currentTimeMillis()));params.put("goods_id_list", "[" + goodsId + "]");params.put("with_coupon", "true");// 生成签名params.put("sign", generateSign(params));// 构造请求 URLStringBuilder urlBuilder = new StringBuilder(API_URL);for (Map.Entry<String, String> entry : params.entrySet()) {urlBuilder.append("&").append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8));}// 发送 HTTP 请求try (CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse response = httpClient.execute(new HttpGet(urlBuilder.toString()))) {HttpEntity entity = response.getEntity();if (entity != null) {String responseStr = EntityUtils.toString(entity, StandardCharsets.UTF_8);return JSON.parseObject(responseStr);}}return null;}public static void main(String[] args) throws IOException, ParseException {// 替换为您的 clientId 和 clientSecretPddPriceClient client = new PddPriceClient("your_client_id", "your_client_secret");// 替换为您想要查询的商品 IDJSONObject goodsDetail = client.getGoodsDetail("1234567890");System.out.println(goodsDetail.toJSONString());}
}三、关键技术说明
(一)签名生成逻辑
拼多多 API 签名实现步骤:
将所有参数(含公共参数和业务参数)按参数名 ASCII 码升序排序。
拼接为
key=value&key=value格式。末尾拼接
clientSecret后进行 MD5 加密。加密结果转为大写即为签名值。
(二)核心接口解析
接口名称:
pdd.ddk.goods.detail(拼多多联盟商品详情接口)关键参数:
goods_id_list:商品 ID 列表(JSON 格式字符串,如["1234567890"])with_coupon:是否返回优惠券信息(true/false)pid:推广位 ID(可选,用于获取专属优惠)
核心返回字段:
min_group_price:拼团最低价(即到手价,单位:分)original_price:商品原价(单位:分)coupons:优惠券列表(含优惠金额和使用门槛)
四、使用注意事项
权限申请:
pdd.ddk.goods.detail接口需开通拼多多联盟权限(个人 / 企业均可申请)。限流控制:默认 QPS 为 10,超限返回 429 错误,建议添加请求间隔。
参数格式:
goods_id_list需为 JSON 数组字符串,需用fastjson序列化。推广位 ID:
pid非必需,但传入后可获取推广专属优惠和佣金信息。
该实现可集成到 Java 电商系统中,适用于比价工具、导购平台等场景,通过扩展可支持批量查询和优惠规则展示。
