使用Amazon SP-API分页时重复返回结果,如何正确获取下一页?
Amazon SP-API 分页返回重复结果问题解决
问题场景
使用Amazon Selling Partner API的/listings/2021-08-01/items/{sellerId}接口拉取产品列表,通过nextToken处理分页,但调用时出现循环:每次用返回的新nextToken请求,仍得到和首次相同的产品,同时API会返回新的nextToken。环境为Node.js + Express。
现有代码
async getListingsItems(nextToken?: string): Promise<any> { const path = `/listings/2021-08-01/items/${this.sellerId}`; const params = new URLSearchParams({ marketplaceIds: this.marketplaceId, includedData: 'summaries,attributes,issues,offers,fulfillmentAvailability,procurement', limit: '20', }); if (nextToken) { params.append('nextToken', nextToken); } const queryParams = params.toString(); const headers = await this.authService.createSignedHeaders('GET', path, queryParams); const response = await axios.get(`${this.endpoint}${path}?${queryParams}`, { headers }); return response.data; }
首次请求响应
{ "success": true, "products": 10, "data": { "numberOfResults": 51500, "pagination": { "nextToken": "someTokenHere" }, "items": [ { "sku": "acs-1524", ... } ] } }
问题解答
1. 是否遗漏了Amazon SP-API的分页处理规则?
是的,你忽略了SP-API分页的核心规则:
nextToken包含完整请求上下文:nextToken是API生成的加密串,已经包含了首次请求的所有参数(limit、marketplaceIds、includedData等)和分页位置信息。当使用nextToken请求下一页时,不能再传入其他查询参数,否则会覆盖nextToken中的上下文,导致API重新从第一页返回结果。- 签名需严格匹配请求参数:SP-API的签名依赖请求的完整路径和查询参数,如果每次请求都携带所有参数+
nextToken,签名逻辑会生成和API预期不符的签名,导致分页上下文失效。
2. 如何确保获取下一页产品而非重复结果?
修改代码的参数构建逻辑,区分首次请求和分页请求:
async getListingsItems(nextToken?: string): Promise<any> { const path = `/listings/2021-08-01/items/${this.sellerId}`; const params = new URLSearchParams(); if (nextToken) { // 分页请求只传nextToken params.set('nextToken', nextToken); } else { // 首次请求传完整参数 params.set('marketplaceIds', this.marketplaceId); params.set('includedData', 'summaries,attributes,issues,offers,fulfillmentAvailability,procurement'); params.set('limit', '20'); } const queryParams = params.toString(); const headers = await this.authService.createSignedHeaders('GET', path, queryParams); const response = await axios.get(`${this.endpoint}${path}?${queryParams}`, { headers }); return response.data; }
额外注意事项:
- 每次使用最新
nextToken:必须用当前请求返回的pagination.nextToken发起下一次请求,不能复用旧的nextToken。 - 验证签名逻辑:确认
createSignedHeaders方法正确使用了当前请求的queryParams生成签名,签名不匹配会导致API拒绝分页上下文。 - 检查
marketplaceIds格式:确保传入的是单个合法的 marketplace ID 字符串,若传数组需确认API支持多市场查询,格式错误也会导致分页异常。
内容的提问来源于stack exchange,提问作者Shahidur Rahman Nayeem
相关产品推荐
相关产品推荐

