Fix WooCommerce auth: use query params instead of Basic auth header (proven working via curl)
This commit is contained in:
@@ -9,16 +9,25 @@ module.exports = function (RED) {
|
|||||||
const node = this;
|
const node = this;
|
||||||
|
|
||||||
node.baseUrl = (config.baseUrl || "https://example.com/wp-json/wc/v3").replace(/\/+$/, "");
|
node.baseUrl = (config.baseUrl || "https://example.com/wp-json/wc/v3").replace(/\/+$/, "");
|
||||||
|
// Credentials from node-RED credentials tab OR from config node fields
|
||||||
node.consumerKey = (this.credentials && this.credentials.consumerKey) || config.consumerKey || "";
|
node.consumerKey = (this.credentials && this.credentials.consumerKey) || config.consumerKey || "";
|
||||||
node.consumerSecret = (this.credentials && this.credentials.consumerSecret) || config.consumerSecret || "";
|
node.consumerSecret = (this.credentials && this.credentials.consumerSecret) || config.consumerSecret || "";
|
||||||
|
|
||||||
node.sendRequest = function (method, path, body, msgHeaders, callback) {
|
node.sendRequest = function (method, path, body, msgHeaders, callback) {
|
||||||
const url = new URL(node.baseUrl + path);
|
const url = new URL(node.baseUrl + path);
|
||||||
|
|
||||||
|
// Append consumer_key and consumer_secret as query parameters (proven working in curl)
|
||||||
|
if (node.consumerKey) {
|
||||||
|
url.searchParams.set('consumer_key', node.consumerKey);
|
||||||
|
}
|
||||||
|
if (node.consumerSecret) {
|
||||||
|
url.searchParams.set('consumer_secret', node.consumerSecret);
|
||||||
|
}
|
||||||
|
|
||||||
const parsedBody = body !== undefined ? (typeof body === "string" ? body : JSON.stringify(body)) : undefined;
|
const parsedBody = body !== undefined ? (typeof body === "string" ? body : JSON.stringify(body)) : undefined;
|
||||||
|
|
||||||
const headers = Object.assign({ "Accept": "application/json", "Content-Type": "application/json" }, msgHeaders || {});
|
const headers = Object.assign({ "Accept": "application/json", "Content-Type": "application/json" }, msgHeaders || {});
|
||||||
if (parsedBody !== undefined) headers["Content-Length"] = Buffer.byteLength(parsedBody);
|
if (parsedBody !== undefined) headers["Content-Length"] = Buffer.byteLength(parsedBody);
|
||||||
headers["Authorization"] = "Basic " + Buffer.from(node.consumerKey + ":" + node.consumerSecret).toString("base64");
|
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
method, hostname: url.hostname,
|
method, hostname: url.hostname,
|
||||||
@@ -54,4 +63,4 @@ module.exports = function (RED) {
|
|||||||
consumerSecret: { type: "password" }
|
consumerSecret: { type: "password" }
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user