You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 the if/else block. 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: 201 on success, but 201 is for resource creation. Fetching a list should use 200 to match Shopify's successful response.
  • Potential Authentication Misconfiguration: The request library's oauth option is designed for OAuth 1.0a, but Shopify private apps typically use a simpler header-based auth with X-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_webhooks permission 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-Token header to confirm the token works and returns webhooks. This helps rule out code-specific issues.
  • Check Shop Domain: Double-check that the shop variable doesn't include extra characters (like .myshopify.com already appended) to avoid invalid URLs.

内容的提问来源于stack exchange,提问作者Nicolas T

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:39:43