【问题标题】:React Native axios api call ShopifyReact Native axios api 调用 Shopify
【发布时间】:2023-04-10 07:19:02
【问题描述】:

大家早上好,我尝试使用这种方法从 Shopify 获取数据。但它不起作用。

请求失败,状态码 400

可以分享一下你的小经验吗? 我正在开发 React Native 项目。

const api_key = "example-api-key";
    const password = "example-password";
    const version = "2021-07";
    const url = `https://${api_key}:${password}@store-example.myshopify.com/admin/api/${version}/products.json`;
    useEffect(() => {
        axios({
            method:'get',
            url:url
        }).then((result) => {
            console.log(result.data)
        }).catch(error => {
            console.log(error)
        })
    });

【问题讨论】:

  • 您收到错误信息还是数据为空?解释问题。
  • @HymnZzy 嗨,兄弟,它返回错误:请求失败,状态码为 400。
  • 尝试将凭据作为基本身份验证标头传递,而不是在 url 中添加密钥和密码。
  • 确实,因为当我使用 Basic Auth 对 Postman 进行测试时,它会返回数据。在这种情况下,我不知道如何用 axios bro 实现代码。请你帮帮我。

标签: reactjs react-native axios shopify shopify-app


【解决方案1】:

很可能是身份验证失败。将 auth 参数移动到 axios 标头。试试这个

const username = "example-api-key";
const password = "example-password";
const version = "2021-07";
const url = `https://store-example.myshopify.com/admin/api/${version}/products.json`;
useEffect(() => {
    axios({
        method:'get',
        url,
        auth: { username,password }
    }).then((result) => {
        console.log(result.data)
    }).catch(error => {
        console.log(error)
    })
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 2021-12-28
    • 2020-05-02
    • 1970-01-01
    相关资源
    最近更新 更多