简体   繁体   English

从 shopify 主题扩展调用 GET API 时出现 404 错误

[英]404 error calling GET API from a shopify theme-extension

I'm new to shopify development, and can't figure out how to call an authenticated API from a shopify theme-extension.我是 shopify 开发的新手,无法弄清楚如何从 shopify 主题扩展调用经过身份验证的 API。 Essentially, I'm trying to make a theme extension, where one of the functionalities is that when a checkbox is clicked, an API that counts the number of products is called.本质上,我正在尝试进行主题扩展,其中一项功能是单击复选框时,将调用计算产品数量的 API。

I have a working api that gets the product count, and in web>index.js , I have set-up the end-point:我有一个工作 api 获取产品计数,并且在web>index.js中,我已经设置了终点:

app.get("/api/products/count", async (_req, res) => {
  const countData = await shopify.api.rest.Product.count({
    session: res.locals.shopify.session,
  });
  res.status(200).send(countData);
});

Under web>frontend>hooks , I have the authenticated hooks set-up as shown below.web>frontend>hooks下,我设置了经过身份验证的挂钩,如下所示。 I've tested that if I call the "api/products/count" API from one of the web pages using useAppQuery , it works as expected, and returns the product count.我已经测试过,如果我使用 useAppQuery 从 web 页面之一调用“api/products/count” useAppQuery ,它会按预期工作,并返回产品计数。

import { useAuthenticatedFetch } from "./useAuthenticatedFetch";
import { useMemo } from "react";
import { useQuery } from "react-query";

export const useAppQuery = ({ url, fetchInit = {}, reactQueryOptions }) => {
  const authenticatedFetch = useAuthenticatedFetch();
  const fetch = useMemo(() => {
    return async () => {
      const response = await authenticatedFetch(url, fetchInit);
      return response.json();
    };
  }, [url, JSON.stringify(fetchInit)]);

  return useQuery(url, fetch, {
    ...reactQueryOptions,
    refetchOnWindowFocus: false,
  });
};

In my theme extension code, I've added an event listener to the checkbox which calls getProductCount.在我的主题扩展代码中,我向调用 getProductCount 的复选框添加了一个事件侦听器。 In getProductCount , I want to call /api/products/count:getProductCount中,我想调用 /api/products/count:

import { useAppQuery } from "../../../web/frontend/hooks";

export const getProductCount = (product) => {
  const {
    data,
    refetch: refetchProductCount,
    isLoading: isLoadingCount,
    isRefetching: isRefetchingCount,
  } = useAppQuery({
    url: "/api/products/count",
    reactQueryOptions: {
      onSuccess: () => {
        setIsLoading(false);
      },
    },
  });
}


However, when I run locally and click the checkbox, it returns a 404 error trying to find useAppQuery .但是,当我在本地运行并单击复选框时,它会返回 404 错误以尝试查找useAppQuery The request URL is https://cdn.shopify.com/web/frontend/hooks .请求 URL 是https://cdn.shopify.com/web/frontend/hooks It seems like the authentication isn't working because that URL looks incorrect.似乎身份验证不起作用,因为 URL 看起来不正确。

Am I missing a step that I need to do in order to call an authenticated API from a theme-extension?我是否缺少从主题扩展中调用经过身份验证的 API 所需执行的步骤?

I thought the issue was just the import path for useAppQuery but I've tried different paths, and they all return the same 404 issue.我认为问题只是useAppQuery的导入路径,但我尝试了不同的路径,它们都返回相同的 404 问题。

If you want a hot tip here.如果你想在这里得到一个热点提示。 In your theme App extension, you do not actually need to make an API call to get a product count.在您的主题应用程序扩展中,您实际上不需要拨打 API 电话来获取产品数量。 In your theme app extension, you can just use Liquid, and dump the product count out to a variable of your choice, and use the count, display the count, do whatever.在您的主题应用程序扩展中,您可以只使用 Liquid,并将产品计数转储到您选择的变量,然后使用计数、显示计数,做任何事情。

{{ shop.product_count }}

Of course, this does not help you if you need other storefront API calls in your theme App extension, but whatever.当然,如果您需要在您的主题 App 扩展中调用其他店面 API,这对您没有帮助,但无论如何。 In my experience, I render the API Access Token I need in my theme app extension, and then making my Storefront API calls is just a fetch().根据我的经验,我在主题应用程序扩展中呈现我需要的 API 访问令牌,然后调用店面 API 只是一个 fetch()。

The only time I would use authenticated fetch, is when I am doing embedded App API calls, but that is a different beast from a theme app extension.我唯一一次使用经过身份验证的提取是在我进行嵌入式应用程序 API 调用时,但这与主题应用程序扩展不同。 In there, you do not get to make authenticated calls as the front-end is verboten for those of course.在那里,您无法进行经过身份验证的调用,因为前端当然是禁止调用的。 Instead you'd use App Proxy for security.相反,您会使用 App Proxy 来确保安全。

TL:DR;长话短说:博士; Storefront API calls with a token should not fail with a 404 if you call the right endpoint.如果调用正确的端点,使用令牌的店面 API 调用不应失败并显示 404。 You can use Storefront API inside a theme app extension.您可以在主题应用程序扩展中使用 Storefront API。 Inside a theme app extension, if you need backend Admin API access, you can use App Proxy calls.在主题应用程序扩展中,如果您需要后端 Admin API 访问权限,您可以使用 App Proxy 调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM