调用 shopify api 时出现“未提供商店”响应

问题描述 投票:0回答:1

在商店中获取主题列表时遇到一些麻烦。我已经安装了新版本的 shopify 应用程序,如 shopify 开发文档所示。

当我控制台记录响应时,我得到这个:

我是否遗漏了一些明显或重要的东西?

shopify-app shopify-api shopify-api-node
1个回答
0
投票

不清楚,从你分享的代码来看,你用的是什么样的

fetch
。 您应该使用模板提供的获取:

import {useAuthenticatedFetch} from "./hooks/index.js";
const fetch = useAuthenticatedFetch();

我在这里报告以获得完整的答案:

export function useAuthenticatedFetch() {
  const app = useAppBridge();
  const fetchFunction = authenticatedFetch(app);

  return async (uri, options) => {
    const response = await fetchFunction(uri, options);
    checkHeadersForReauthorization(response.headers, app);
    return response;
  };
}

function checkHeadersForReauthorization(headers, app) {
  if (headers.get("X-Shopify-API-Request-Failure-Reauthorize") === "1") {
    const authUrlHeader =
      headers.get("X-Shopify-API-Request-Failure-Reauthorize-Url") ||
      `/api/auth`;

    const redirect = Redirect.create(app);
    redirect.dispatch(
      Redirect.Action.REMOTE,
      authUrlHeader.startsWith("/")
        ? `https://${window.location.host}${authUrlHeader}`
        : authUrlHeader
    );
  }
}

© www.soinside.com 2019 - 2024. All rights reserved.