一文搞懂拼多多API接口开发

746 阅读7分钟

拼多多开放平台为开发者提供了丰富的API接口,允许第三方应用获取平台商品、订单、物流等数据。本文以商品详情为例,展示如何调用拼多多API接口。

一、准备工作

1. 申请开发者账号

2. 了解接口权限

  • 商品详情接口需要"商品信息查询"权限
  • 新应用默认有基础权限,部分高级权限需额外申请

二、获取商品详情的核心API

1. 接口基本信息

  • 接口名称:pdd.item_get
  • 请求方式:HTTP POST
  • 数据格式:JSON

2. 必备请求参数

参数名类型是否必须说明
keyString调用key
sercetString调用密钥
num_iidString商品ID列表,多个用逗号分隔
api_nameStringAPI接口名称[item_get]

三、API调用实战

1. 请求示例

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.PrintWriter;
import java.net.URLConnection;

public class Example {
	private static String readAll(Reader rd) throws IOException {
		StringBuilder sb = new StringBuilder();
		int cp;
		while ((cp = rd.read()) != -1) {
			sb.append((char) cp);
		}
		return  sb.toString();
	}
	public static JSONObject postRequestFromUrl(String url, String body) throws IOException, JSONException {
		URL realUrl = new URL(url);
		URLConnection conn = realUrl.openConnection();
		conn.setDoOutput(true);
		conn.setDoInput(true);
		PrintWriter out = new PrintWriter(conn.getOutputStream());
		out.print(body);
		out.flush();
		InputStream instream = conn.getInputStream();
		try {
			BufferedReader rd = new BufferedReader(new InputStreamReader(instream, Charset.forName("UTF-8")));
			String jsonText = readAll(rd);
			JSONObject json = new JSONObject(jsonText);
			return json;
		} finally {
			instream.close();
		}
	}
	public static JSONObject getRequestFromUrl(String url) throws IOException, JSONException {
		URL realUrl = new URL(url);
		URLConnection conn = realUrl.openConnection();
		InputStream instream = conn.getInputStream();
		try {
			BufferedReader rd = new BufferedReader(new InputStreamReader(instream, Charset.forName("UTF-8")));
			String jsonText = readAll(rd);
			JSONObject json = new JSONObject(jsonText);
			return json;
		} finally {
			instream.close();
		}
	}
	public static void main(String[] args) throws IOException, JSONException {
		// 请求示例 url 默认请求参数已经URL编码处理
		String url = "https://api-gw.fan-b.com/pinduoduo/item_get/?key=<您自己的apiKey>&secret=<您自己的apiSecret>&num_iid=1620002566";
		JSONObject json = getRequestFromUrl(url);
		System.out.println(json.toString());
	}

}

2. 返回结果解析

成功响应示例:

{
    "items":{
        "page":"10",
        "real_total_results":2000,
        "total_results":2000,
        "page_size":20,
        "pagecount":100,
        "_ddf":"eow",
        "item":[
            {
                "title":"牛胸肉50斤 巴西进口牛肉面馆用原切切片牛肉冷冻牛肉牛胸肉商用",
                "pic_url":"https://img.pddpic.com/mms-material-img/2024-01-28/b0254222-e8ba-4918-9a0f-6c316e8e5e72.jpeg",
                "promotion_price":"1097.8",
                "price":"1097.8",
                "sales":8,
                "num_iid":586280148663,
                "seller_id":117895202,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=586280148663"
            },
            {
                "title":"潮汕牛肉火锅食材吊龙嫩肉肥牛组合套餐",
                "pic_url":"https://img.pddpic.com/mms-material-img/2024-01-29/853b3645-db8f-4fe7-a37b-1c3181ab3df0.jpeg",
                "promotion_price":"47.6",
                "price":"47.6",
                "sales":16,
                "num_iid":586324736363,
                "seller_id":666613519,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=586324736363"
            },
            {
                "title":"李丫头牛肉 沈丘特产10包200G 另有沈丘李丫头牛腱  包邮【2月28日发完】",
                "pic_url":"https://img.pddpic.com/mms-material-img/2024-01-29/74f9b58f-28a3-4646-8e1c-d36a1a17838e.jpeg",
                "promotion_price":"386.44",
                "price":"386.44",
                "sales":11,
                "num_iid":586441874448,
                "seller_id":901836469,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=586441874448"
            },
            {
                "title":"湘当经典湘西牛肉风味丝408g大礼包湖南特产张家界景区同款素牛肉",
                "pic_url":"https://img.pddpic.com/mms-material-img/2024-01-28/23a1abd9-565a-4b12-abbc-858109a4bb8e.jpeg",
                "promotion_price":"36.5",
                "price":"36.5",
                "sales":2,
                "num_iid":586280069640,
                "seller_id":150283384,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=586280069640"
            },
            {
                "title":"玉林牛腊巴广西玉林特产香辣牛肉条麻辣牛巴传统手艺味道独特包邮",
                "pic_url":"https://t00img.yangkeduo.com/goods/images/2021-03-21/48d4a63a-ea96-4b61-a01e-088113ecf610.jpeg",
                "promotion_price":"58.4",
                "price":"58.4",
                "sales":0,
                "num_iid":585679909771,
                "seller_id":965132469,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=585679909771"
            },
            {
                "title":"大促销 生牛肉10斤",
                "pic_url":"https://img.pddpic.com/mms-material-img/2024-01-01/f1f83e65-9ae8-4106-9a45-6dc2e984bc0d.jpeg",
                "promotion_price":"430.02",
                "price":"430.02",
                "sales":1,
                "num_iid":576488968007,
                "seller_id":577575374,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=576488968007"
            },
            {
                "title":"酱卤牛肉熟食真空包装内蒙古特产零食营养休闲小吃即食下酒菜",
                "pic_url":"https://img.pddpic.com/mms-material-img/2024-01-25/aad85671-f6dc-46d2-8dec-9d0a831b14e0.jpeg",
                "promotion_price":"33",
                "price":"33",
                "sales":14,
                "num_iid":585527238777,
                "seller_id":335213461,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=585527238777"
            },
            {
                "title":"前后阿兰那腱子50斤,当天拍发货很快,海南偏远地区不发",
                "pic_url":"https://img.pddpic.com/mms-material-img/2024-01-06/437c7863-c314-43fe-9e3d-5de4413a93c5.jpeg",
                "promotion_price":"778",
                "price":"778",
                "sales":18,
                "num_iid":578236178968,
                "seller_id":136934121,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=578236178968"
            },
            {
                "title":"西牛优选黄牛牛排骨5斤黄牛草饲散养牛肋排牛排牛肉新鲜黄膘散养3",
                "pic_url":"https://img.pddpic.com/mms-material-img/2023-08-07/8dd5ba7c-4611-474b-85ee-ad784bcb316a.jpeg",
                "promotion_price":"104.69",
                "price":"104.69",
                "sales":300000,
                "num_iid":234200007672,
                "seller_id":869944360,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=234200007672"
            },
            {
                "title":"原切牛肉干牛肉粒网红吃货办公室即熟食零食休闲特色小吃休闲食品",
                "pic_url":"https://img.pddpic.com/mms-material-img/2021-07-19/25d93394-ea75-4b23-a93d-67cc9c09d0b5.png.a.jpeg",
                "promotion_price":"4.8",
                "price":"4.8",
                "sales":1000000,
                "num_iid":255631497340,
                "seller_id":529792881,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=255631497340"
            },
            {
                "title":"御牛满地澳洲安格斯牛排原切谷饲M3板腱雪花牛排健身减脂牡蛎肉",
                "pic_url":"https://img.pddpic.com/mms-material-img/2023-09-14/b5b3eba7-dbd8-47a7-b957-405d4e911292.jpeg",
                "promotion_price":"59",
                "price":"59",
                "sales":5435,
                "num_iid":528243175682,
                "seller_id":923098940,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=528243175682"
            },
            {
                "title":"巴西雪花牛肉原装进口大雪花烤肉涮肉牛尖峰牛肉火锅寿喜锅健身",
                "pic_url":"https://img.pddpic.com/mms-material-img/2023-05-15/f70ae76c-284f-4ade-9fcd-069c30827dce.jpeg",
                "promotion_price":"108",
                "price":"108",
                "sales":17000,
                "num_iid":482651563151,
                "seller_id":804788346,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=482651563151"
            },
            {
                "title":"真牛肉内蒙古正宗手撕风干牛肉干新鲜牛肉零食独立小包装无干燥剂",
                "pic_url":"https://img.pddpic.com/mms-material-img/2024-01-24/60cb16d7-a468-46c9-97df-d82a4a592da6.jpeg",
                "promotion_price":"9.9",
                "price":"9.9",
                "sales":13,
                "num_iid":585161165639,
                "seller_id":258613228,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=585161165639"
            },
            {
                "title":"牦牛肉青藏高原正品牦牛肉正宗清真牛肋骨里脊牛腩牛腱牛腿肉新鲜",
                "pic_url":"https://img.pddpic.com/mms-material-img/2024-01-12/babb0509-9470-4dab-8cb5-26488b26ac5a.jpeg",
                "promotion_price":"188",
                "price":"188",
                "sales":290,
                "num_iid":580689837817,
                "seller_id":690308329,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=580689837817"
            },
            {
                "title":"小炒黄牛肉嫩滑小炒牛肉200g/包生鲜免调理懒人菜新鲜原切牛肉片",
                "pic_url":"https://img.pddpic.com/mms-material-img/2023-07-25/1ea2b6ab-e249-4ac0-a8fd-a3ab1afc7ca0.png",
                "promotion_price":"29.3",
                "price":"29.3",
                "sales":1016,
                "num_iid":506249661178,
                "seller_id":314561002,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=506249661178"
            },
            {
                "title":"正宗内蒙古牛肉干独立包装手撕7成干即食50克休闲零食风干牛肉干",
                "pic_url":"https://img.pddpic.com/mms-material-img/2024-01-24/d1d85396-9baf-4d81-a630-6de31e505bde.jpeg",
                "promotion_price":"9.9",
                "price":"9.9",
                "sales":6,
                "num_iid":585143732343,
                "seller_id":326843543,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=585143732343"
            },
            {
                "title":"【小牛推荐】批发现称新鲜原切牛肋条一整箱农家散养黄牛肉零添加",
                "pic_url":"https://commimg.pddpic.com/ad-promotion/2023-12-27/7c978bc4-23f7-4b56-b325-1fbbee9084d3.jpeg",
                "promotion_price":"119.5",
                "price":"119.5",
                "sales":300000,
                "num_iid":258911037877,
                "seller_id":964996078,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=258911037877"
            },
            {
                "title":"宁夏正宗清真泾源新鲜小牛肉原切牛腱子牛腩牛里脊5/3/2/1斤包邮",
                "pic_url":"https://t00img.yangkeduo.com/goods/images/2020-03-27/0f31bb68-1c8b-4390-b11f-d952864b8780.jpg",
                "promotion_price":"121",
                "price":"121",
                "sales":1705,
                "num_iid":99158732108,
                "seller_id":910250516,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=99158732108"
            },
            {
                "title":"包邮批发新鲜正宗冷冻原切新鲜现杀鲜肉生鲜肉后新鲜生鲜肉一整箱",
                "pic_url":"https://img.pddpic.com/mms-material-img/2022-10-20/00e56ca0-d5f6-4d00-8ffe-6db4a99a97d4.jpeg",
                "promotion_price":"45.58",
                "price":"45.58",
                "sales":17000,
                "num_iid":424359929045,
                "seller_id":154153856,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=424359929045"
            },
            {
                "title":"希菲 4斤原切牛腩块0添加谷饲牛腩肉块卤肉炖煮食材生鲜年货礼盒",
                "pic_url":"https://img.pddpic.com/mms-material-img/2023-12-21/fc1c21a1-bdfd-4ac6-8186-c8aca38aefe3.png",
                "promotion_price":"85.49",
                "price":"85.49",
                "sales":28000,
                "num_iid":235553835351,
                "seller_id":508708250,
                "detail_url":"https://mobile.yangkeduo.com/item.htm?id=235553835351"
            }
        ]
    },
    "error_code":"0000",
    "reason":"ok",
    "secache":"b904f63c1d1b6c539a40028cdff22e1f",
    "secache_time":1706756190,
    "secache_date":"2024-02-01 10:56:30",
    "translate_status":"",
    "translate_time":0,
    "language":{
        "default_lang":"cn",
        "current_lang":"cn"
    },
    "error":"",
    "cache":0,
    "api_info":"today: max:15000 all[=++];expires:2031-01-01",
    "execution_time":"0.45",
    "server_time":"Beijing/2024-02-01 10:56:30",
    "client_ip":"127.0.0.1",
    "call_args":{
        "q":"牛肉",
        "start_price":"0",
        "end_price":"0",
        "page":"10",
        "cat":"0"
    },
    "api_type":"pinduoduo",
    "server_memory":"3.15MB",
    "last_id":false
}

四、常见问题解决方案

1. 签名错误

  • 检查参数排序是否正确
  • 确认App Secret无误
  • 时间戳是否在有效期内(通常5分钟内)

2. 权限不足

  • 检查应用是否已申请相应权限
  • 确认access_token有效(如需)

3. 限流处理

  • 拼多多API默认QPS为50
  • 超出限制会返回错误码,需做好请求队列和重试机制

五、高级应用技巧

  1. 批量获取:单次请求最多支持100个商品ID
  2. 字段过滤:通过fields参数指定需要返回的字段
  3. 缓存策略:商品数据可缓存1-2小时,减少API调用
  4. 错误监控:建立API调用日志,监控成功率

六、安全注意事项

  1. App Secret必须严格保密
  2. 敏感数据需加密存储
  3. 生产环境使用HTTPS协议
  4. 定期轮换Access Token

实际开发中,建议参考官方最新文档,因为接口可能会进行版本更新和调整。欢迎大家一起交流学习!