Reputation: 14
I am trying to get my WordPress woocommerce order data first time with nodejs and followed this https://github.com/woocommerce/woocommerce-rest-api-js-lib woocommerce documentation but am unable to get data on localhost:5000/page. The data is coming with postman but is unable to get on the localhost page.
The response data is:
code: 'woocommerce_rest_cannot_view'
message: 'Sorry, you cannot view this resource.'
data: { status: 401 }
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const cors = require("cors");
const mysql = require("mysql2");
const WooCommerceRestApi = require("@woocommerce/woocommerce-rest-api").default;
const api = new WooCommerceRestApi({
url: "https://fasbazar.com",
consumerKey: "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
consumerSecret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
version: "wc/v3"
});
app.use(cors());
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true }));
const db = mysql.createPool({
host: "localhost",
user: "root",
password: "",
database: "restapi"
});
// List products
api
.get("orders/1", {
per_page: 20 // 20 products per page
})
.then(response => {
// Successful request
console.log("Response Status:", response.status);
console.log("Response Headers:", response.headers);
console.log("Response Data:", response.data);
console.log("Total of pages:", response.headers["x-wp-totalpages"]);
console.log("Total of items:", response.headers["x-wp-total"]);
})
.catch(error => {
// Invalid request, for 4xx and 5xx statuses
console.log("Response Status:", error.response.status);
console.log("Response Headers:", error.response.headers);
console.log("Response Data:", error.response.data);
})
.finally(() => {
// Always executed.
});
app.listen(5000, () => {
console.log("Server is running on port 5000");
});
The output: output image
Upvotes: 0
Views: 1660
Reputation: 2444
I extracted the error from the screenshot of your reponse (and added it to your question, as this is the most relevant information): woocommerce_rest_cannot_view
(401)
Add queryStringAuth
:
const WooCommerce = new WooCommerceRestApi({
url: 'https://example.com',
consumerKey: 'consumer_key',
consumerSecret: 'consumer_secret',
version: 'wc/v3', // Also add the version!
queryStringAuth: true // Force Basic Authentication as query string true and using under HTTPS
});
Source: https://woocommerce.github.io/woocommerce-rest-api-docs/?javascript#authentication-over-https (code samples - Node.js tab)
If that does not work, consult this article on GitHub/woocommerce provides troubleshooting on the 401 problem: https://github.com/woocommerce/woocommerce/wiki/Getting-started-with-the-REST-API#401-unauthorized
It states:
401 Unauthorized
Your API keys or signature is wrong. Ensure that:
- The user you generated API keys for actually has access to those resources.
- The username when authenticating is your consumer key.
- The password when authenticating is your consumer secret.
- Make a new set of keys to be sure.
If your server utilizes FastCGI, check that your authorization headers are properly read.
Make sure you read the complete article.
You can furthermore check all the existing Stack Overflow posts addressing 'woocommerce_rest_cannot_view':
https://stackoverflow.com/search?q=woocommerce_rest_cannot_view
Upvotes: 1