获取Vue中Axios的Shopify Admin端点

发布于 2025-02-04 12:33:11 字数 1582 浏览 4 评论 0原文

你怎么做?

从Shopify获取产品

<template>
    <div>{{ data }}</div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'ShopifyResult',
  data () {
    return {
      data: null
    }
  },
  mounted () {
    const url = 'https://a-shopify-store.myshopify.com/admin/api/2021-10/checkouts.json';
    axios({
        method:'get',
        url,
        headers: {
        "Content-Type": "application/json",
        "X-Shopify-Access-Token": 'API_ADMIN_ACCESS_TOKEN_HERE',
        },
    }).then((result) => {
        console.log(result.data)
    }).catch(error => {
        console.log(error)
    });
  }
}
</script>

这就是我得到的:

访问xmlhttprequest at 'https://a-shopify-store.myshopify.com/admin/api/2021-10/checkouts.json' 从原点'http:// localhost:8080'被CORS策略所阻止: 对飞行前请求的响应不会传递访问控制检查:否 请求的“访问控制”标头 资源。 shopifyresult.vue?4048:27 axioserror

为什么涉及CORS?我有一个适当的钥匙。你怎么做?

我还发现

https://community.shopify.com/c/technical-qa/vue-axios-get-requests-how-to-secure-secure-secure-apikey-apikey-and-apipass/td-pd-p/689016

没有答案.....

How do you do this?

Use Admin API from Frontend(Headless Commerce) to get products from Shopify

<template>
    <div>{{ data }}</div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'ShopifyResult',
  data () {
    return {
      data: null
    }
  },
  mounted () {
    const url = 'https://a-shopify-store.myshopify.com/admin/api/2021-10/checkouts.json';
    axios({
        method:'get',
        url,
        headers: {
        "Content-Type": "application/json",
        "X-Shopify-Access-Token": 'API_ADMIN_ACCESS_TOKEN_HERE',
        },
    }).then((result) => {
        console.log(result.data)
    }).catch(error => {
        console.log(error)
    });
  }
}
</script>

This is what I get:

Access to XMLHttpRequest at
'https://a-shopify-store.myshopify.com/admin/api/2021-10/checkouts.json'
from origin 'http://localhost:8080' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested
resource. ShopifyResult.vue?4048:27 AxiosError

Why is CORS involved? I have a all the appropriate keys. How do you do this?

I also found

https://community.shopify.com/c/technical-q-a/vue-axios-get-requests-how-to-secure-apikey-and-apipass/td-p/689016

no answers.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新人笑 2025-02-11 12:33:11

https://community.shopify.com/c/shopify-apis-and-sdks/ususe-apis-from-a-different-origin-domain/td-p/502781

这样想:

StoreFront API是公共API,它的令牌是
在客户端使用。

admin API是私人API,令牌并不意味着共享
公开。如果您通过从
浏览器,他们可以潜在地访问有关的私人信息
您的商店,财务信息,客户信息等

这可能会给您的客户带来不便
如果要遭受欺诈,如果您可能会遇到法律问题
有人决定因此起诉您。

因此,Shopify检测到您使用的时会丢下CORS错误
浏览器将请求发送到私人API。虽然CORS错误可能
在这里不是最合适的回应,对他们来说是有效的
拒绝您的请求。

希望能阐明其他遇到此错误的问题。

https://community.shopify.com/c/shopify-apis-and-sdks/using-apis-from-a-different-origin-domain/td-p/502781

Think of it this way:

Storefront API is a public API and the token for it is meant to be
used on the client side.

Admin API is a private API and the token is not meant to be shared
publicly. If you share this token to everyone by making requests from
the browser, they can potentially access private information about
your store, financial information, CUSTOMER information etc.

This could lead to both inconvenience to your customers as they would
be subject to fraud as well as you could run into legal issues if
someone decides to sue you because of it.

Hence, Shopify throws a CORS error when detecting that you use a
browser to send a request to a private API. While the CORS error might
not be the most appropriate response here, it is valid for them to
deny your request.

Hope that clarifies the issue for others encountering this error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文