前言 在工业电商数字化对接场景中,震坤行作为 MRO(维护、维修与运营)核心平台,其商品详情接口是打通供应链数据、实现库存同步与精准选品的关键。不同于普通电商接口,震坤行工业品接口具备数据字段复杂、规格参数结构化、权限校验严格三大特性。本文将从认证机制、接口封装、响应解析到异常重试,提供一套可直接落地的实战方案,规避通用对接中的常见坑点。 接口地址: 请求方式:GET 数据格式:JSON(UTF-8) 认证方式:OAuth2.0(Access Token) 调用限制:单应用 QPS=10,日调用上限 10 万次 对接前需在震坤行开放平台注册应用,获取 python python 表格 问题:令牌过期导致 401 错误 解决:工具类内置自动刷新,提前 60 秒校验有效期 问题:规格参数乱码 / 缺失 解决:响应头指定 问题:高频调用触发 429 限流 解决:添加 QPS 限流控制(≤10),采用指数退避重试机制一、接口基础与认证准备
1.1 接口核心信息
https://openapi.zkh360.com/api/v2/products/{sku}1.2 认证流程(获取 Access Token)
client_id和client_secret,通过客户端模式获取令牌,令牌有效期默认 2 小时。二、核心代码实现(Python)
2.1 认证工具类(含自动刷新)
运行import requests
import time
from datetime import datetime
class ZkhAuth:
def __init__(self, client_id, client_secret):
self.client_id = client_id
self.client_secret = client_secret
self.token_url = "https://openapi.zkh360.com/oauth/token"
self.access_token = None
self.expires_at = 0
def get_token(self):
# 令牌未过期直接返回
if self.access_token and time.time() < self.expires_at - 60:
return self.access_token
# 重新获取令牌
params = {
"grant_type": "client_credentials",
"client_id": self.client_id,
"client_secret": self.client_secret
}
try:
resp = requests.post(self.token_url, data=params, timeout=10)
resp.raise_for_status()
data = resp.json()
self.access_token = data["access_token"]
self.expires_at = time.time() + data["expires_in"]
return self.access_token
except Exception as e:
raise Exception(f"认证失败:{str(e)}")2.2 商品详情接口封装(含异常处理)
运行class ZkhProductApi:
def __init__(self, auth):
self.auth = auth
self.base_url = "https://openapi.zkh360.com/api/v2/products"
def get_product_detail(self, sku, retry=2):
"""
获取商品详情
:param sku: 商品SKU(如"AA6375379")
:param retry: 重试次数
:return: 结构化商品数据
"""
token = self.auth.get_token()
url = f"{self.base_url}/{sku}"
headers = {"Authorization": f"Bearer {token}"}
for attempt in range(retry + 1):
try:
resp = requests.get(url, headers=headers, timeout=15)
resp.raise_for_status()
result = resp.json()
# 业务状态校验
if result.get("code") != 0:
raise Exception(f"业务异常:{result.get('message')}")
return self._parse_detail(result["data"])
except Exception as e:
if attempt < retry:
time.sleep(2 ** attempt) # 指数退避重试
continue
raise Exception(f"获取详情失败(重试{retry}次):{str(e)}")
def _parse_detail(self, raw_data):
"""结构化解析核心字段(过滤冗余数据)"""
return {
"sku": raw_data["sku"],
"name": raw_data["productName"],
"brand": raw_data["brandName"],
"price": {
"sale_price": raw_data["salePrice"],
"market_price": raw_data["marketPrice"]
},
"stock": raw_data["stockNum"],
"spec": raw_data["specification"],
"images": [img["url"] for img in raw_data["imageList"]],
"category": raw_data["categoryName"],
"update_time": datetime.fromtimestamp(raw_data["updateTime"]/1000).strftime("%Y-%m-%d %H:%M:%S")
}
# 使用示例
if __name__ == "__main__":
# 替换为你的client_id和client_secret
auth = ZkhAuth("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET")
product_api = ZkhProductApi(auth)
try:
# 查询指定SKU商品
product = product_api.get_product_detail("AA6375379")
print("商品详情:")
for k, v in product.items():
print(f"{k}: {v}")
except Exception as e:
print(f"错误:{e}")三、响应结果解析(关键字段说明)
3.1 核心返回字段
字段 类型 说明 sku String 商品唯一编码(核心查询标识) productName String 商品全称(含型号规格) salePrice BigDecimal 销售价(含税) stockNum Integer 实时库存(含分仓数据) specification String 规格参数(JSON 字符串,需二次解析) imageList Array 商品主图 / 细节图(高清无水印) 3.2 工业场景特殊字段
technicalParameters:技术参数(如材质、耐压值、精度等级)certificationList:认证信息(如 CE、ISO、防爆认证)deliveryCycle:交货周期(工业品核心关注字段)四、常见坑点与解决方案
4.1 认证相关
4.2 数据异常
charset=utf-8,解析时处理 JSON 转义字符4.3 限流处理