微店商品详情API接口
微店(Weidian)提供了一系列的API接口用于获取商品信息,以下是主要的商品相关API接口及其使用方法。
1. 获取商品详情接口
请求URL
bash
https://v2.weidian.com/item/get
请求方式
POST
请求参数
参数名 | 类型 | 必填 | 描述 |
---|---|---|---|
access_token | string | 是 | 授权后获得的access_token |
item_id | string | 是 | 商品ID |
fields | string | 否 | 需要返回的字段,多个字段用逗号分隔 |
请求示例
python
python
import requests
import json
def get_weidian_item_detail(access_token, item_id):
url = "https://v2.weidian.com/item/get"
headers = {
"Content-Type": "application/json"
}
data = {
"access_token": access_token,
"item_id": item_id,
"fields": "item_id,title,price,stock,images,desc,skus"
}
response = requests.post(url, headers=headers, data=json.dumps(data))
return response.json()
# 使用示例
# access_token = "your_access_token" # 替换为实际的access_token
# item_id = "123456789" # 替换为实际商品ID
# detail = get_weidian_item_detail(access_token, item_id)
# print(json.dumps(detail, indent=2, ensure_ascii=False))
返回数据示例,API接口测试
json
json
{
"status": {
"status_code": 0,
"status_reason": ""
},
"data": {
"item_id": "123456789",
"title": "商品标题",
"price": "99.00",
"original_price": "199.00",
"stock": 100,
"images": [
"https://wd.image.weidian.com/123456789/1.jpg",
"https://wd.image.weidian.com/123456789/2.jpg"
],
"desc": "商品描述HTML内容",
"skus": [
{
"sku_id": "sku123",
"title": "红色-XL",
"price": "99.00",
"stock": 50,
"props": [
{"name": "颜色", "value": "红色"},
{"name": "尺寸", "value": "XL"}
]
},
{
"sku_id": "sku456",
"title": "蓝色-L",
"price": "89.00",
"stock": 30,
"props": [
{"name": "颜色", "value": "蓝色"},
{"name": "尺寸", "value": "L"}
]
}
],
"category": {
"id": "cat123",
"name": "服装"
},
"postage": "10.00",
"is_virtual": false,
"created_time": 1634567890,
"updated_time": 1634567890
}
}
2. 获取商品列表接口
请求URL
bash
https://v2.weidian.com/item/list
请求方式
POST
请求参数
参数名 | 类型 | 必填 | 描述 |
---|---|---|---|
access_token | string | 是 | 授权后获得的access_token |
page_num | int | 否 | 页码,默认1 |
page_size | int | 否 | 每页数量,默认20,最大100 |
status | int | 否 | 商品状态:0全部 1出售中 2已售罄 3仓库中 |
请求示例
ini
python
def get_weidian_item_list(access_token, page_num=1, page_size=20, status=0):
url = "https://v2.weidian.com/item/list"
headers = {
"Content-Type": "application/json"
}
data = {
"access_token": access_token,
"page_num": page_num,
"page_size": page_size,
"status": status
}
response = requests.post(url, headers=headers, data=json.dumps(data))
return response.json()
# 使用示例
# item_list = get_weidian_item_list(access_token)
# print(json.dumps(item_list, indent=2, ensure_ascii=False))
3. 获取商品SKU信息接口
请求URL
bash
https://v2.weidian.com/item/sku/get
请求方式
POST
请求参数
参数名 | 类型 | 必填 | 描述 |
---|---|---|---|
access_token | string | 是 | 授权后获得的access_token |
item_id | string | 是 | 商品ID |
请求示例
ini
python
def get_weidian_item_skus(access_token, item_id):
url = "https://v2.weidian.com/item/sku/get"
headers = {
"Content-Type": "application/json"
}
data = {
"access_token": access_token,
"item_id": item_id
}
response = requests.post(url, headers=headers, data=json.dumps(data))
return response.json()
# 使用示例
# skus = get_weidian_item_skus(access_token, item_id)
# print(json.dumps(skus, indent=2, ensure_ascii=False))
4. 获取商品评价接口
请求URL
bash
https://v2.weidian.com/item/rate/list
请求方式
POST
请求参数
参数名 | 类型 | 必填 | 描述 |
---|---|---|---|
access_token | string | 是 | 授权后获得的access_token |
item_id | string | 是 | 商品ID |
page_num | int | 否 | 页码,默认1 |
page_size | int | 否 | 每页数量,默认20,最大100 |
请求示例
ini
python
def get_weidian_item_rates(access_token, item_id, page_num=1, page_size=20):
url = "https://v2.weidian.com/item/rate/list"
headers = {
"Content-Type": "application/json"
}
data = {
"access_token": access_token,
"item_id": item_id,
"page_num": page_num,
"page_size": page_size
}
response = requests.post(url, headers=headers, data=json.dumps(data))
return response.json()
# 使用示例
# rates = get_weidian_item_rates(access_token, item_id)
# print(json.dumps(rates, indent=2, ensure_ascii=False))
认证与授权
在使用微店API前,需要先获取access_token:
-
获取授权URL:
bashhttps://v2.weidian.com/oauth2/authorize
参数:
- client_id: 应用ID
- response_type: code
- redirect_uri: 回调地址
- state: 自定义参数
-
获取access_token:
kotlinpython def get_weidian_access_token(client_id, client_secret, code): url = "https://v2.weidian.com/oauth2/access_token" data = { "client_id": client_id, "client_secret": client_secret, "grant_type": "authorization_code", "code": code } response = requests.post(url, data=data) return response.json()