Reputation: 827
I'm using Wordpress 4.7.1
with Woocommerce 2.6.13
plugin with enabled REST API. I was created user credentials from console plugin UI with read_write
permissions.
Now I'm trying GET products using OAuth1: service return 200 OK, credentials are right.
then I'm trying to DELETE some product: or trying to create new product: service return 401 Unauthorized.
Whats wrong?
UPD1: .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
# END WordPress
Upvotes: 13
Views: 5301
Reputation: 379
I've had the same problem with WC REST API and solved it using v2 and instead of PUT I used POST, e.g.:
$woocommerce = new Client(
'*** woocommerce-website-url ***',
'*** key ***',
'*** secret ***',
[
'version' => 'wc/v2',
'verify_ssl' => false,
'wp_api' => true,
'query_string_auth' => true
]
);
$data = [
"stock_quantity" => "0"
];
$woocommerce->post('products/12345', $data);
Upvotes: 0
Reputation: 487
This https://github.com/woocommerce/woocommerce/wiki/Getting-started-with-the-REST-API#server-does-not-support-postdeleteput do the trick for me.
Some times, Server does not support POST/DELETE/PUT Ideally, your server should be configured to accept these types of API request, but if not you can use the _method property.
Doing a POST request, and passing _method=PUT as a query parameter works for me.
Upvotes: 2
Reputation: 123
You are using an old version API method. Use the latest version with the updated woocommerce plugin.You can also follow this link https://woocommerce.github.io/woocommerce-rest-api-docs/?php#delete-a-product
Upvotes: 1
Reputation: 8699
What are the roles of the user that is associated with the API keys?
I had some authorization errors with the Woocommerce API, even though the API keys had read/write permissions. After I checked the Woocommerce API keys settings in Wordpress, I noticed that the user that was associated with the keys I used, didn't have any Woocommerce roles, such as Customer, Shop Manager or Admin.
After associating an admin user to some new API keys, I resolved the issue and could authorise with all API endpoints.
Since you are authorised for only some of the endpoints, it could be the same issue. The user that is associated with the API keys probably doesn't have a role with permissions to delete.
Upvotes: 0