1688商品API全链路开发实践
一、接口认证与权限体系
OAuth2.0鉴权流程
# 获取access_token示例 import requests auth_url = "https://gw.open.1688.com/auth/authorize" params = { "client_id": "YOUR_APP_KEY", "redirect_uri": "CALLBACK_URL", "state": "anti_csrf_token" } response = requests.get(auth_url, params=params) # 需处理302跳转获取code
关键点:
企业认证账号才可调用商品列表接口
每日调用限额5000次(需申请提升)
点击获取key和secret
二、商品列表接口逆向分析
分页陷阱处理
官方文档未说明的隐藏参数:
{ "pageSize": 50, // 实际最大支持100 "pageNo": 1, "orderBy": "gmtCreate:desc", // 非公开排序字段 "filterParams": "isRts=1" // 过滤现货商品 }
数据清洗方案
def clean_product_data(raw_item): # 处理价格单位换算 price = float(raw_item['priceInfo']['amount']) / 10**raw_item['priceInfo']['decimals'] # 提取规格参数(嵌套JSON字符串需二次解析) spec = json.loads(raw_item['productSpecs']) if isinstance(raw_item['productSpecs'], str) else {} return { 'item_id': raw_item['productId'], 'title': raw_item['subject'].strip(), 'price': price, 'spec': spec }
三、高并发优化策略
本地缓存设计
from datetime import timedelta from django.core.cache import caches class ProductCache: def __init__(self): self.backend = caches['redis'] def get_items(self, shop_id): key = f"1688_products_{shop_id}" items = self.backend.get(key) if not items: items = fetch_api_data(shop_id) # 封装API请求 self.backend.set(key, items, timeout=timedelta(hours=1)) return items
四、异常处理清单
错误码1006:商品类目权限未开通
错误码2021:IP白名单未配置
数据字段isDeleted=1时需主动过滤下架商品
五、合规调用建议
严禁伪造User-Agent为浏览器
商品数据缓存时间不超过24小时(协议要求)
必须展示1688官方商品详情页链接