1. 准备工作
-
创建应用并申请权限:
- 进入"控制台-应用管理",创建应用(类型选"第三方应用"或"企业自用应用")。
- 申请接口权限:核心接口为
taobao.item.reviews.get
(商品评论列表)、taobao.traderates.get
(交易评价数据)。 - 审核通过后获取
app_key
和app_secret
(需妥善保管)。
2. 接口调用核心参数
参数名 | 必选 | 说明 | 示例值 |
---|---|---|---|
method |
是 | 接口名称 | taobao.item.reviews.get |
num_iid |
是 | 商品ID(从商品URL提取) | 123456789 |
page_no |
否 | 分页页码(默认1) | 1 |
page_size |
否 | 每页评论数(默认20,最大100) | 50 |
sort |
否 | 排序方式(时间/评分) | create_time:desc |
sign_method |
是 | 签名算法(MD5/HMAC-SHA256) | hmac |
timestamp |
是 | 时间戳(格式:YYYY-MM-DD HH:MM:SS ) |
2025-09-10 12:00:00 |
3. 签名生成算法
以HMAC-SHA256为例:
python
python
import hmac
import hashlib
def generate_sign(params, app_secret):
sorted_params = sorted(params.items()) # 按参数名ASCII升序排序
query_str = '&'.join([f"{k}={v}" for k, v in sorted_params if k != 'sign']) # 拼接字符串
sign_str = f"{app_secret}{query_str}{app_secret}" # 前后拼接App Secret
return hmac.new(app_secret.encode(), sign_str.encode(), hashlib.sha256).hexdigest().upper()
4. 完整调用代码示例(Python)
python
python
import requests
import time
APP_KEY = "YOUR_APP_KEY"
APP_SECRET = "YOUR_APP_SECRET"
ITEM_ID = "123456789" # 商品ID
API_URL = "https://eco.taobao.com/router/rest"
# 构造请求参数
params = {
"method": "taobao.item.reviews.get",
"app_key": APP_KEY,
"num_iid": ITEM_ID,
"page_no": 1,
"page_size": 50,
"sort": "create_time:desc",
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
"format": "json",
"v": "2.0",
"sign_method": "hmac"
}
# 生成签名并添加到参数
params["sign"] = generate_sign(params, APP_SECRET)
# 发送请求
response = requests.get(API_URL, params=params)
result = response.json()
# 解析JSON数据
if "item_reviews_get_response" in result:
reviews = result["item_reviews_get_response"]["reviews"]["review"]
for review in reviews:
print(f"用户昵称: {review['user_nick']}")
print(f"评论时间: {review['created']}")
print(f"评分: {review['score']}星")
print(f"评论内容: {review['content']}")
if "pictures" in review:
print(f"晒图链接: {review['pictures'][0]}")
else:
print(f"请求失败: {result.get('error_response', '未知错误')}")
5. JSON数据结构示例
成功响应的JSON格式如下:
css
json
{
"item_reviews_get_response": {
"total_results": "4605",
"reviews": {
"review": [
{
"user_nick": "用户123",
"content": "商品质量很好,物流也很快!",
"score": "5",
"created": "2025-03-25 10:00:00",
"pictures": ["http://example.com/image1.jpg"],
"reply": {
"content": "感谢您的支持!",
"reply_time": "2025-03-25 12:00:00"
}
}
]
}
}
}