开发 fmode-amazon-sp-api 模块,集成亚马逊 Selling Partner API (SP-API),首期对接客户反馈 (Customer Feedback) 相关接口。
注意:本模块采用 2023 年 10 月 2 日后的简化授权流程,无需 AWS IAM 配置,仅需 LWA Access Token。
getItemReviewTopicshttps://sellingpartnerapi-na.amazon.com/customerFeedback/2024-06-01/items/{asin}/reviews/topicsGETdocs/亚马逊需要对接的接口.mdmodules/fmode-amazon-sp-api/
├── src/
│ ├── mod.ts # 主模块导出
│ ├── client.ts # SP-API 基础客户端(处理 LWA 认证)
│ ├── api/
│ │ └── customerFeedback.ts # Customer Feedback API 实现
│ ├── types.ts # 类型定义
│ └── routes.ts # Express 路由定义
├── docs/
│ ├── task.md # 任务文档
│ ├── config.md # 配置说明
│ └── 亚马逊需要对接的接口.md # 需求文档
├── deno.json # Deno 配置
├── package.json # 包元数据
└── README.md # 项目文档
定义 SP-API 配置接口、请求参数接口和响应数据接口。
export interface SpApiConfig {
clientId: string;
clientSecret: string;
refreshToken: string;
region: string; // e.g., 'us-east-1'
sandbox?: boolean; // 可选:是否使用沙箱
}
export interface GetItemReviewTopicsParams {
asin: string;
itemName?: string;
marketplaceId: string;
countryCode?: string;
dateRange?: {
startDate: string;
endDate: string;
};
}
实现 SpApiClient 类,封装 SP-API 通用逻辑:
x-amz-access-token。实现 CustomerFeedbackApi 类,继承或使用 SpApiClient:
async getItemReviewTopics(params: GetItemReviewTopicsParams): 调用 getItemReviewTopics 接口。实现以下端点:
| 方法 | 路径 | 描述 |
|---|---|---|
| GET | /customerFeedback/reviews/topics | 获取商品评论主题 |
| GET | /health | 健康状态检查 |
导出所有公共接口:
SpApiClient 类CustomerFeedbackApi 类createSpApiRoutes 函数package.json: 依赖管理 (axios)deno.json: Deno 兼容性配置在 api/routes.ts 中添加:
import { createSpApiRoutes } from "../modules/fmode-amazon-sp-api/src/mod.ts";
const spApiConfig = {
clientId: process.env.SP_API_CLIENT_ID,
clientSecret: process.env.SP_API_CLIENT_SECRET,
refreshToken: process.env.SP_API_REFRESH_TOKEN,
region: "us-east-1"
};
router.use("/amazon-sp-api", createSpApiRoutes(spApiConfig));
curl http://localhost:10003/api/amazon-sp-api/health
curl "http://localhost:10003/api/amazon-sp-api/customerFeedback/reviews/topics?asin=B0BT5K9B2T&marketplaceId=ATVPDKIKX0DER"
asin 和 marketplaceId 等必填参数。[SP-API] Request: GET /customerFeedback/2024-06-01/items/B0BT5K9B2T/reviews/topics
[SP-API] Params: {"marketplaceId": "ATVPDKIKX0DER"}
[SP-API] Response: 200 OK
成功响应:
{
"success": true,
"data": {
"asin": "B0BT5K9B2T",
"topics": {
"positiveTopics": [...],
"negativeTopics": [...]
}
}
}
错误响应:
{
"success": false,
"error": {
"code": "InvalidInput",
"message": "The ASIN is invalid."
}
}