“cannot list resources” error from WooCommerce REST API

大城市里の小女人 提交于 2019-12-30 09:57:04

问题


I am very new to WordPres` and using the WooCommerce plugin for the first time and want to use the WooCommerce REST API in my Android app.

I have enabled the REST API and created a Consumer Key as well as a Consumer secret.

Now, when am using this url http://www.my-domain.com/wp-json/wc/v1/products/ in my browser to check if the API is working or not, I get this error:

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

SSL is not installed on my server. How can I fix this? Do I need to use the PHP Client Library?


回答1:


For the "new" v2 API (not the "legacy v2" API):

  • For secure connections (HTTPS), you need to use "Basic Authentication".
  • For non-secure connections (HTTP), you need to use "oAuth1 Authentication".

If you use the wrong authentication with the wrong connection, you'll get the 401 response message.

This is covered in the WooCommerce API documentation: https://woocommerce.github.io/woocommerce-rest-api-docs/#authentication-over-https

Also note that normally you would pass the CK and CS in as header fields, but depending on how the server is configured, you may find that it will not parse the header fields correctly, so you may get success if you include the CK and CS in the URL as parameters:

https://website.tld/wp-json/wc/v1/products?consumer_key=[key]&consumer_secret=[secret]




回答2:


You are getting a 401 response from the server. This response indicates the you are not authorized for that REST endpoint. In order to use the woocommerce api you need to include your authorization in the request.

There are examples and documentation here: http://woocommerce.github.io/woocommerce-rest-api-docs/#authentication

The woocommerce examples don't include java examples. (I assume that is what you are using for your android app). However there is a 3rd party java library for woocommerce. It can be found here: https://github.com/icoderman/wc-api-java

I have not personally used that library, but it should contain all the functions you will need to authorize and correctly request data from the woocommerce api.



来源:https://stackoverflow.com/questions/42593704/cannot-list-resources-error-from-woocommerce-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!