当前位置: 首页 > news >正文

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>

(二)认证信息

从拼多多开放平台获取 clientIdclientSecret,推广位 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 签名实现步骤:

  1. 将所有参数(含公共参数和业务参数)按参数名 ASCII 码升序排序。

  2. 拼接为 key=value&key=value 格式。

  3. 末尾拼接 clientSecret 后进行 MD5 加密。

  4. 加密结果转为大写即为签名值。

(二)核心接口解析

  • 接口名称pdd.ddk.goods.detail(拼多多联盟商品详情接口)

  • 关键参数

    • goods_id_list:商品 ID 列表(JSON 格式字符串,如 ["1234567890"]

    • with_coupon:是否返回优惠券信息(true/false

    • pid:推广位 ID(可选,用于获取专属优惠)

  • 核心返回字段

    • min_group_price:拼团最低价(即到手价,单位:分)

    • original_price:商品原价(单位:分)

    • coupons:优惠券列表(含优惠金额和使用门槛)

四、使用注意事项

  1. 权限申请pdd.ddk.goods.detail 接口需开通拼多多联盟权限(个人 / 企业均可申请)。

  2. 限流控制:默认 QPS 为 10,超限返回 429 错误,建议添加请求间隔。

  3. 参数格式goods_id_list 需为 JSON 数组字符串,需用 fastjson 序列化。

  4. 推广位 IDpid 非必需,但传入后可获取推广专属优惠和佣金信息。

该实现可集成到 Java 电商系统中,适用于比价工具、导购平台等场景,通过扩展可支持批量查询和优惠规则展示。

http://www.dtcms.com/a/578312.html

相关文章:

  • 邢台市做网站电话wordpress用户名可以修改
  • 成都有几家做网站的公司小型crm系统
  • 工程计算 - Mathcad® 替代方案
  • 广州网站设计建设网站改版 重新收录
  • ESP32C3初应用:点灯及ADC
  • 301 是什么意思?——HTTP 状态码详解与应用
  • 深入解析cursor Token消耗详细分解
  • 深圳网站设计联系电话广告公司海报用的易拉
  • fiddler模拟弱网延时请求
  • 基于单片机的剧本杀场景控制系统(论文+源码)
  • mysql启动提示1067:进程意外终止
  • 网站建设的服务器郑州app制作
  • 智能决策算法的核心原理是什么?
  • springboot基于Java的校园导航微信小程序的设计与实现(代码+数据库+LW)
  • 11.大模型Agent应用
  • 学院网站建设目的与意义手机商城在哪里找到
  • MySQL的GROUP_CONCAT函数详解
  • Temu平台新规全面收紧,卖家如何破局迎战年终大促?
  • 底层视觉及图像增强-项目实践-细节再<十六-9,如何用AI实现LED显示画质增强:总结再回顾>:从LED大屏,到手机小屏,快来挖一挖里面都有什么
  • 怎么做网站后缀识别符号才不会变电脑淘宝网页版
  • 网站访客qq获取系统 报价陕西建设局官方网站
  • 免费做电子书的网站有哪些win2008系统做网站
  • SQL中的函数索引/表达式索引
  • 上海房地产网站建设报价响应式网站开发 三合一建站
  • 中企动力建设网站做网站图片要求
  • TCP协议深度解析:从三次握手到可靠传输的底层机制
  • numpy___数组/图像形状改变(transpose和reshape详解)
  • 【TestNG自动化测试框架详解】
  • 怎么查询备案号商城网站建设优化推广
  • [特殊字符] Vue3 项目最佳实践:组件命名、目录结构与类型规范指南