使用POST和GET时出现WooCommerce Rest API错误

时间:2016-10-27 09:09:44

标签: javascript php wordpress woocommerce woocommerce-rest-api

在那里我尝试使用来自woocommerce的api customers,当我尝试使用它时,我总是得到回应: 网址:http://example.com/wp-json/wc/v1/customers

{
  "code": "woocommerce_rest_cannot_view",
  "message": "Sorry, you cannot list resources.",
  "data": {
    "status": 401
  }
}


{
  "code": "woocommerce_rest_authentication_error",
  "message": "Invalid Signature - provided signature does not match.",
  "data": {
    "status": 401
  }
}

有时候:

和创建:

{
  "code": "woocommerce_rest_cannot_create",
  "message": "Sorry, you are not allowed to create resources.",
  "data": {
    "status": 401
  }
}

我该怎么办?

我已经添加了密钥:

here

或者我只能拥有1把钥匙? : here

或者我需要更新其中一个脚本?

希望,有人可以帮助我解决我的问题

3 个答案:

答案 0 :(得分:1)

我遇到了这个确切的问题,但是根据我的经验,我有好消息和坏消息,有时当我没有做任何更改时,API会关闭我,有时重新启动NGINX服务器会解决这个问题。

目前我与邮递员和OAuth 1.0的连接产生以下结果;

{
  "code": "woocommerce_rest_cannot_view",
  "message": "Sorry, you cannot list resources.",
  "data": {
    "status": 401
  }
}

有了我们的方式:

  1. 确保'键入'在postman中设置为OAuth 1.0。
  2. 确保您的网址正确无误(http或https)。
  3. 正如我上面所说的那样,我从来没有设法使我保持稳定,并且它会在不改变任何事情的情况下辍学。

    我是如何解决这个问题的方法是使用'基本身份验证'用户名your_ck,密码your_cs。我相信您需要使用SSL来使用Basic Auth,但设置SSL并不是一件大事,所以它绝对值得。

    如果您确实发现OAuth 1.0失败的原因,我很乐意听到。

    编辑:有许多人遇到此问题的例子example with image可能会对您有所帮助。但似乎大多数人都接受了Basic Auth。

答案 1 :(得分:0)

确保已向用户授予读取/写入访问权限,并且还检查了根目录中的.htaccess文件。尝试在.htaccess文件中使用此代码

# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

答案 2 :(得分:0)

我也遇到了同样的问题,我在 Postman、PHP、NodeJs 中尝试过,几乎花了 2 天但没有输出,最后在 URL 末尾加上斜杠后开始得到响应,

我的意思是,

最初我使用的是 https://example.com/wp-json/wc/v3/products?consumer_key=ck_XXXX&consumer_secret=cs_XXXX 但是把它改成这样https://example.com/wp-json/wc/v3/products/?consumer_key=ck_XXXX&consumer_secret=cs_XXXX(只需在查询字符串开头的问号前加一个斜线)。

在 Postman 中,即使没有设置任何授权设置(Type = No Auth),我也开始收到回复。

它可能与我的 .htaccess 有某种关系,但我觉得如果我也在这里发布这个解决方案会很好。