Shopify Admin API调用持续返回404错误,寻求解决方案
问题:Shopify Admin API调用持续返回404 Not Found
场景与代码
开发Discord机器人时,使用@shopify/admin-api-client调用Shopify Admin API,无论是查询指定订单还是调用products/count端点,均返回404 Not Found。核心代码如下:
const { SlashCommandBuilder } = require('discord.js'); const { createAdminRestApiClient } = require('@shopify/admin-api-client'); const client = createAdminRestApiClient({ storeDomain: 'x.myshopify.com', apiVersion: '2024-04', accessToken: 'y', }); module.exports = { data: new SlashCommandBuilder() .setName('order') .setDescription('Returns a order with a given order id.') .addIntegerOption(option => option.setName('id') .setDescription('The id of the order you want info about!') .setRequired(true)), async execute(interaction) { // 初始调用订单接口 const orderId = interaction.options.getInteger('id'); const response = await client.get(`orders/${orderId}`); console.log(response) if (response.ok) { const body = await response.json(); console.log(body); } // 后续测试调用products/count接口 // const response = await client.get(`products/count`); // console.log(response) // if (response.ok) { // const body = await response.json(); // console.log(body); // } }, };
错误现象
两种接口调用均返回404响应,典型响应内容如下:
Response { [Symbol(realm)]: null, [Symbol(state)]: { aborted: false, rangeRequested: false, timingAllowPassed: true, requestIncludesCredentials: true, type: 'default', status: 404, timingInfo: { startTime: 530421.735029, redirectStartTime: 0, redirectEndTime: 0, postRedirectStartTime: 530421.735029, finalServiceWorkerStartTime: 0, finalNetworkResponseStartTime: 0, finalNetworkRequestStartTime: 0, endTime: 0, encodedBodySize: 26, decodedBodySize: 0, finalConnectionTimingInfo: null }, cacheState: '', statusText: 'Not Found', headersList: HeadersList { cookies: null, [Symbol(headers map)]: [Map], [Symbol(headers map sorted)]: null }, urlList: [ URL {} ], body: { stream: undefined } }, [Symbol(headers)]: HeadersList { cookies: null, [Symbol(headers map)]: Map(25) { 'date' => [Object], 'content-type' => [Object], 'transfer-encoding' => [Object], 'connection' => [Object], 'x-sorting-hat-podid' => [Object], 'vary' => [Object], 'referrer-policy' => [Object], 'x-frame-options' => [Object], 'x-request-id' => [Object], 'server-timing' => [Object], 'x-shopify-stage' => [Object], 'content-security-policy' => [Object], 'x-content-type-options' => [Object], 'x-download-options' => [Object], 'x-permitted-cross-domain-policies' => [Object], 'x-xss-protection' => [Object], 'x-envoy-upstream-service-time' => [Object], 'x-dc' => [Object], 'cf-cache-status' => [Object], 'report-to' => [Object], 'nel' => [Object], 'server' => [Object], 'cf-ray' => [Object], 'content-encoding' => [Object], 'alt-svc' => [Object] }, [Symbol(headers map sorted)]: null } }
排查尝试
- 更换为有效订单ID,仍返回404
- 切换到
products/count接口,排除订单ID错误的可能,依旧返回404
问题分析与解决方案
1. 端点路径格式问题
Shopify Admin API的REST端点需要遵循/admin/api/{apiVersion}/{resource}.json的格式,部分场景下@shopify/admin-api-client不会自动添加.json后缀,导致路径无效。尝试修改调用路径:
// 订单查询 const response = await client.get(`orders/${orderId}.json`); // 产品计数 const response = await client.get(`products/count.json`);
2. Access Token权限与有效性
- 确认使用的是Shopify Admin API Access Token,而非Storefront API Token
- 检查Token是否包含所需权限:查询订单需要
read_orders权限,查询产品计数需要read_products权限 - 验证Token有效性:直接用Postman调用相同端点测试,确认Token能正常访问
3. Store Domain格式问题
确保storeDomain参数为完整的店铺域名(如your-store.myshopify.com),不要添加https://前缀,客户端会自动处理协议部分。
4. API版本兼容性
确认使用的2024-04版本API是否支持对应的端点,Shopify会定期淘汰旧版本API,若版本过旧可能导致端点不可用。可以尝试切换到最新稳定版本(如2024-07)测试。
5. 客户端初始化参数检查
检查createAdminRestApiClient的参数是否存在拼写错误(如apiVersion写成小写),确保所有配置项都正确传入。
内容的提问来源于stack exchange,提问作者Tay Baye
相关产品推荐
相关产品推荐

