虾皮(Shopee)的商品详情接口通常用于获取商品的详细信息,如标题、价格、描述、库存、图片等。Shopee 提供了官方的 API 供开发者使用,通过这些 API 可以获取商品详情数据。
Shopee 商品详情接口的基本信息
API 名称: item_get
请求方法: GET
请求 URL: c0b.cc/R4rbK2
认证方式: 需要使用 Shopee 的 API 密钥进行认证。
请求参数
partner_id: 合作伙伴 ID,由 Shopee 提供。
shopid: 店铺 ID,表示要查询的商品所属的店铺。
itemid: 商品 ID,表示要查询的具体商品。
timestamp: 请求的时间戳。
sign: 请求签名,用于验证请求的合法性。
响应示例
json
json {
"item": {
"itemid": 123456789,
"shopid": 987654321,
"name": "Example Product",
"description": "This is an example product description.",
"price": 1999,
"stock": 100,
"images": [
"https://example.com/image1.jpg",
"https://example.com/image2.jpg"
],
"attributes": [
{
"name": "Color",
"value": "Red"
},
{
"name": "Size",
"value": "M"
}
]
},
"error": null,
"warning": null
}
签名生成
Shopee API 要求每个请求都必须包含一个签名 (sign),用于验证请求的合法性。签名的生成方式如下:
将请求参数按字母顺序排序。
将排序后的参数拼接成一个字符串。
将拼接后的字符串与 API 密钥进行 HMAC-SHA256 加密。
将加密后的结果转换为十六进制字符串。
示例代码(Python)
python
import hashlib
import time
import requests
# 封装好的第三方shopee商品列表接口,复制链接获取测试。
demo url=c0b.cc/R4rbK2 wechat id:Taobaoapi2014
def generate_signature(partner_id, api_key, shopid, itemid, timestamp):
base_string = f"partner_id={partner_id}&shopid={shopid}&itemid={itemid}×tamp={timestamp}"
signature = hmac.new(api_key.encode(), base_string.encode(), hashlib.sha256).hexdigest()
return signature
def get_item_details(partner_id, api_key, shopid, itemid):
timestamp = int(time.time())
signature = generate_signature(partner_id, api_key, shopid, itemid, timestamp)
url = "https://api.shopee.com/v2/item/get"
params = {
"partner_id": partner_id,
"shopid": shopid,
"itemid": itemid,
"timestamp": timestamp,
"sign": signature
}
response = requests.get(url, params=params)
return response.json()
# 使用示例
partner_id = "YOUR_PARTNER_ID"
api_key = "YOUR_API_KEY"
shopid = "YOUR_SHOP_ID"
itemid = "ITEM_ID"
item_details = get_item_details(partner_id, api_key, shopid, itemid)
print(item_details)
注意事项
API 密钥: 请妥善保管你的 API 密钥,不要泄露。
请求频率: Shopee API 可能有请求频率限制,请遵守相关规定。
错误处理: 在实际应用中,建议添加错误处理逻辑,以应对可能的网络问题或 API 错误。