Shopify私有应用调用GET /admin/webhooks.json接口报错求助
Troubleshooting and Fixing Shopify Webhook List Fetch Error
Let's break down the issues in your code and fix them step by step:
Key Issues in Your Current Code
- Promise State Conflict: Your
reject('Could not get customer activation URL')is placed outside theif/elseblock. This means no matter if the request succeeds or fails, this reject will fire after the resolve. Since a Promise can only settle once, this causes an unhandled Promise rejection error, which is almost certainly triggering your failure. - Missing Shopify API Version: Shopify's Admin API now requires an explicit API version in the URL. The old versionless path (
/admin/webhooks.json) is deprecated—you need to use a format like/admin/api/2024-07/webhooks.json(replace with the latest supported version from Shopify's docs). - Uncaught JSON Parse Error: If Shopify's response body isn't valid JSON,
JSON.parse(body)will throw an uncaught error that crashes your code. - Incorrect Status Code: You're returning
status: 201on success, but 201 is for resource creation. Fetching a list should use200to match Shopify's successful response. - Potential Authentication Misconfiguration: The
requestlibrary'soauthoption is designed for OAuth 1.0a, but Shopify private apps typically use a simpler header-based auth withX-Shopify-Access-Token. This is more reliable and avoids compatibility issues with the library's OAuth handling.
Fixed Code Implementation
const request = require('request'); function fetchShopifyWebhooks(shop, accessToken) { return new Promise((resolve, reject) => { // Use a supported Shopify API version (check Shopify docs for latest) const apiVersion = '2024-07'; const apiUrl = `https://${shop}.myshopify.com/admin/api/${apiVersion}/webhooks.json`; request.get({ url: apiUrl, headers: { 'X-Shopify-Access-Token': accessToken, 'Content-Type': 'application/json' } }, (error, response, body) => { // Handle network/request-level errors first if (error) { return reject({ error: 'Failed to connect to Shopify API', details: error }); } // Safely parse JSON response let webhookData; try { webhookData = JSON.parse(body); } catch (parseErr) { return reject({ error: 'Failed to parse Shopify API response', details: parseErr, rawResponseBody: body }); } // Validate response status if (response.statusCode === 200) { return resolve({ webhook: webhookData, status: 200 }); } else { // Include Shopify's error details for easier debugging return reject({ error: 'Shopify API returned an error', status: response.statusCode, shopifyErrorDetails: webhookData, rawResponse: response }); } }); }); }
Additional Troubleshooting Steps
- Verify Permissions: Ensure your private app has the
read_webhookspermission enabled in the Shopify admin (under Apps > Manage private apps > Your App > Admin API permissions). - Test with Postman: Send a manual GET request to the API URL with the
X-Shopify-Access-Tokenheader to confirm the token works and returns webhooks. This helps rule out code-specific issues. - Check Shop Domain: Double-check that the
shopvariable doesn't include extra characters (like.myshopify.comalready appended) to avoid invalid URLs.
内容的提问来源于stack exchange,提问作者Nicolas T
相关产品推荐
相关产品推荐

