Woocommerce Rest Api (2.6 & later) – 401 error
-
Hello,
I would like to use Woocommerce with Rest API.
So, my process :
-
activate REST API in woocommerce’s settings.
-
Generate keys for an user.
-
Try to authentificate me over HTTP.
The third step works with this:https://packagist.org/packages/automattic/woocommerce
But, I cannot get data, like products.
My code:
require __DIR__ . '/vendor/autoload.php'; use Automattic\WooCommerce\Client; $woocommerce = new Client( 'https://delphes.local', 'ck_ee31d8082f5b6213b369ef5fb1d4f387e0241840', 'cs_479ce18567c180263f130072dea082b52872e6e9', [ 'wp_api' => true, 'version' => 'wc/v1', 'verify_ssl' => false, ] ); var_dump($woocommerce); use Automattic\WooCommerce\HttpClient\HttpClientException; try { $results = $woocommerce->get('products'); // Last request data. $lastRequest = $woocommerce->http->getRequest(); $lastRequest->getUrl(); // Requested URL (string). $lastRequest->getMethod(); // Request method (string). $lastRequest->getParameters(); // Request parameters (array). $lastRequest->getHeaders(); // Request headers (array). $lastRequest->getBody(); // Request body (JSON). // Last response data. $lastResponse = $woocommerce->http->getResponse(); $lastResponse->getCode(); // Response code (int). $lastResponse->getHeaders(); // Response headers (array). $lastResponse->getBody(); // Response body (JSON). echo "ok"; var_dump($results); var_dump($lastRequest); var_dump($lastResponse); }catch (HttpClientException $e) { var_dump($e->getMessage()); // Error message. var_dump($e->getRequest()); // Last request data. var_dump($e->getResponse()); // Last response data. }
And the response:
object(Automattic\WooCommerce\Client)[2906] public 'http' => object(Automattic\WooCommerce\HttpClient\HttpClient)[2907] protected 'ch' => null protected 'url' => string 'https://delphes.local/wp-json/wc/v1/' (length=35) protected 'consumerKey' => string 'ck_ee31d8082f5b6213b369ef5fb1d4f387e0241840' (length=43) protected 'consumerSecret' => string 'cs_479ce18567c180263f130072dea082b52872e6e9' (length=43) protected 'options' => object(Automattic\WooCommerce\HttpClient\Options)[2908] private 'options' => array (size=3) ... private 'request' => null private 'response' => null private 'responseHeaders' => null string 'cURL Error: Operation timed out after 15008 milliseconds with 0 bytes received' (length=78) object(Automattic\WooCommerce\HttpClient\Request)[2909] private 'url' => string 'https://delphes.local/wp-json/wc/v1/products?oauth_consumer_key=ck_ee31d8082f5b6213b369ef5fb1d4f387e0241840&oauth_nonce=fbfd6ee91b9586fa47816b924c79c1c4b1bf3d5c&oauth_signature=PXhShXEu0bgVQuo8pNkLKREUvGcwUyCl593osEF0Akk%3D&oauth_signature_method=HMAC-SHA256&oauth_timestamp=1475658731' (length=284) private 'method' => string 'GET' (length=3) private 'parameters' => array (size=5) 'oauth_consumer_key' => string 'ck_ee31d8082f5b6213b369ef5fb1d4f387e0241840' (length=43) 'oauth_nonce' => string 'fbfd6ee91b9586fa47816b924c79c1c4b1bf3d5c' (length=40) 'oauth_signature' => string 'PXhShXEu0bgVQuo8pNkLKREUvGcwUyCl593osEF0Akk=' (length=44) 'oauth_signature_method' => string 'HMAC-SHA256' (length=11) 'oauth_timestamp' => int 1475658731 private 'headers' => array (size=3) 'Accept' => string 'application/json' (length=16) 'Content-Type' => string 'application/json' (length=16) 'User-Agent' => string 'WooCommerce API Client-PHP/1.1.1' (length=32) private 'body' => string '' (length=0) object(Automattic\WooCommerce\HttpClient\Response)[2911] private 'code' => int 0 private 'headers' => array (size=0) empty private 'body' => boolean false
I have the same response with this at the end:
Warning: Cannot modify header information – headers already sent by (output started at D:\delphes\wp-content\themes\delphes\functions.php:49) in D:\delphes\wp-includes\rest-api\class-wp-rest-server.php on line 1196
AND
{“code”:”woocommerce_rest_cannot_view”,”message”:”Sorry, you cannot list resources”,”data”:{“status”:401}}
I tried too in node.js with this: woocommerce-api (npm package)
With this package, I cannot initialize class by require (by import or require). I have this error :
Uncaught Error: Cannot find module “net”
=> in cookies.js:32 from package “tough-cookie”.
After installation of this missing package, it’s a new error:
Uncaught Error: Cannot find module “../package.json”
in cookies.js:38 ……….
So, I have given up this way.
Now, I’m staying with Automattic.
Maybe, it’s a problem with my vhost, or my htaccess?
I searched for a long time before to write this question.
What do you think about this error 401?
Have a good day for a nice community ??
- The topic ‘Woocommerce Rest Api (2.6 & later) – 401 error’ is closed to new replies.