【问题标题】:Retrieving data from Shopify Storefront API [closed]从 Shopify Storefront API 检索数据 [关闭]
【发布时间】:2019-05-25 03:49:15
【问题描述】:

我提出了一个从 Shopify Storefront API 获取数据的简单请求。

我已确认我的私有应用程序的商店 URL 和 Storefront 访问令牌是正确的。我收到的错误是

{ errors:
   '[API] Invalid API key or access token (unrecognized login or wrong password)' }. 

我在这里错过了什么?

const query = `
    {
      shop {
        name
        primaryDomain {
          url
          host
        }
      }
    }
;


    fetch('https://<store>.myshopify.com/admin/api/graphql', {
      method: 'POST',
      body: JSON.stringify(query),
      headers: {
        'Content-Type': 'application/graphql',
        'Accept': 'application/json',
        "X-Shopify-Storefront-Access-Token": "<storefront-access-token>"
      }
    })
    .then(res => res.json())
    .then(res => console.log(res))
    .catch(response => console.log(response))

【问题讨论】:

  • 检查 devtools 控制台是否有其他错误消息,并使用浏览器 devtools 中的网络窗格来查看该响应实际用于什么请求。如果是针对 OPTIONS 请求而不是代码中的 POST 请求,则表明 CORS 预检 OPTIONS 请求失败——显然是因为 https://&lt;store&gt;.myshopify.com/admin/api/graphql API 端点未启用 CORS。具体来说,这将表明端点在 OPTIONS 请求中期待身份验证凭据。但是浏览器不会在 OPTIONS 请求中发送 -Shopify-Storefront-Access-Token 标头
  • 首先您可以在邮递员中调用此 API,并检查您是否收到响应而不是获取 API 方法中的问题

标签: node.js graphql fetch shopify


【解决方案1】:

你的错误真的很简单。您的 GraphQL 端点是错误的。 Storefront API 与 /admin API 无关,您当然不能从公开验证的应用程序调用

尝试调用正确的端点,您肯定会更好地获取所需数据。

 https://<store>.myshopify.com/api/graphql

所有血淋淋的细节都在这里:https://help.shopify.com/en/api/custom-storefronts/storefront-api/getting-started#accessing-the-storefront-api-graphql-endpoint

【讨论】:

    猜你喜欢
    • 2020-07-08
    • 2020-10-22
    • 2014-02-10
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 2020-06-08
    相关资源
    最近更新 更多