【问题标题】:Woocommerce API per_page max limit in react native反应原生的 Woocommerce API per_page 最大限制
【发布时间】:2020-05-12 00:20:06
【问题描述】:

我正在构建一个带有 react native 和 woocommerce 的电子商务移动应用程序,我正面临这个问题.. per_page 不起作用

这是代码

import WooCommerceAPI from 'react-native-woocommerce-api';

const WooCommerce = new WooCommerceAPI({
  url: 'http://example.com/', // Your store URL
  ssl: false,
  consumerKey: 'ck_xxxxxxxxxxx', // Your consumer key
  consumerSecret: 'cs_xxxxxxxxxxx', // Your consumer secret
  wpAPI: true, // Enable the WP REST API integration
  version: 'wc/v3', // WooCommerce WP REST API version
  queryStringAuth: true
});

然后

  componentDidMount() {
    WooCommerce.get('products?per_page=50', {})
      .then(res => {
        this.setState({
          data: res,
          isLoading: false
        });

      })
      .catch(error => {
        console.log(error);
        this.setState({
          error,
          isLoading: true
        });
      });
  }

然后

    console.log(this.state.data);

我在控制台中得到了这个

对象{ “代码”:“woocommerce_rest_authentication_error”, “数据”:对象{ “状态”:401, }, "message": "无效签名 - 提供的签名不匹配。", }

如果我写WooCommerce.get('products', {}),它会从 woocommerce API 获得 10 个产品作为最大限制,但我需要获得所有产品

【问题讨论】:

  • 其他电话对您有用吗?您收到的错误是 auth 错误,key\secret 不正确或者您没有发送 auth 标头。
  • WooCommerce.get('products', {}) 这行工作正常.. 当我添加这样的参数时会发生此错误WooCommerce.get('products?per_page=50', {})
  • 尝试将参数移动到数据对象中,例如:WooCommerce.get('products', {'per_page': 50}).then(...).catch(...) 另外,如果您使用的是 Windows,请尝试使用 Fiddler 或使用 Chrome DevTools 网络选项卡查看请求的外观(或其等效的Firefox)
  • 我试过这种方式..它带来了这个错误 Object { "code": "woocommerce_rest_authentication_error", "data": Object { "status": 401, }, "message": "Invalid signature - 提供的签名不匹配。", }
  • 有没有办法让你记录请求?发送了哪些标头?

标签: javascript php react-native woocommerce woocommerce-rest-api


【解决方案1】:
 WooCommerce.get('products', {per_page: 50})
  .then(res => {
    this.setState({
      data: res,
      isLoading: false
    });

  })
  .catch(error => {
    console.log(error);
    this.setState({
      error,
      isLoading: true
    });
  });

这应该可行!

【讨论】:

    【解决方案2】:

    node_modules\react-native-woocommerce-api\lib\中打开react-native-woocommerce-api.js

    然后转到第 195 行(_getOAuth().authorize 函数)并更改:

    params.qs = this._getOAuth().authorize({
      url: url,
      method: method
    });
    

    params.qs = this._getOAuth().authorize({
      url: url + '?' + this.join(data, '&'),
      method: method
    });
    

    【讨论】:

    • 不,亲爱的,这是唯一对我有用的解决方案...我搜索了一个多月,这是唯一有效的解决方案..只需将 + '?' + this.join(data, '&'), 添加到 react 内部的 url -native-woocommerce-api.js 文件
    【解决方案3】:
     WooCommerce.get('products?per_page=100&page=1', {}) // YOu can set a Variable in-place of 1 and index 100 is the limit
    

    旧 API 版本

    https://Yoursite/wc-api/v3/products?filter[limit] =-1
    

    希望对你有帮助

    【讨论】:

    • 感谢您的回复...但它没有解决..我仍然遇到同样的错误
    • 什么错误? Object { "code": "woocommerce_rest_authentication_error", "data": Object { "status": 401, }, "message": "Invalid signature - provided signature does not match.", }那个?
    • 是的,亲爱的
    • 好的,这是一个身份验证问题,这意味着您的密码不起作用或您的用户详细信息或类似的错误与“每页产品”过滤器无关
    • 但是当我得到没有类似参数的产品时它工作正常('产品')
    猜你喜欢
    • 2018-04-23
    • 1970-01-01
    • 2018-02-18
    • 2021-12-09
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多