4

Hello I am using WooCommerce API - Node.js Client https://www.npmjs.com/package/woocommerce-api

And I am trying to create a customer which requires a POST request to the server. Here is the code to initialize woocomerece REST API:

var WooCommerceAPI = require('woocommerce-api');

var WooCommerce = new WooCommerceAPI({
  url: 'http://example.com',
  consumerKey: 'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  consumerSecret: 'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  wpAPI: true,
  version: 'wc/v1'
});

This is the code to Create customer:

  var data = {
  email: 'john.doe@example.com',
  first_name: 'John',
  last_name: 'Doe',
  username: 'john.doe',
  billing: {
    first_name: 'John',
    last_name: 'Doe',
    company: '',
    address_1: '969 Market',
    address_2: '',
    city: 'San Francisco',
    state: 'CA',
    postcode: '94103',
    country: 'US',
    email: 'john.doe@example.com',
    phone: '(555) 555-5555'
  },
  shipping: {
    first_name: 'John',
    last_name: 'Doe',
    company: '',
    address_1: '969 Market',
    address_2: '',
    city: 'San Francisco',
    state: 'CA',
    postcode: '94103',
    country: 'US'
  }
};

WooCommerce.post('customers', data, function(err, data, res) {
  console.log(res);
});

But I keep getting the following response from server.

{
"code":"woocommerce_rest_authentication_error",
"message":"Invalid signature - provided signature does not match.",
"data":{"status":401}
}

However any GET request to the works on the server eg: I can get the list of products.

Saad
  • 1,155
  • 3
  • 16
  • 36

6 Answers6

4

As the error is self explanatory. This is clearly an authentication issue. It's clearly mention in the documentation

Over HTTPS

You may use HTTP Basic Auth by providing the API Consumer Key as the username and the API Consumer Secret as the password.

Over HTTP

You must use OAuth 1.0a "one-legged" authentication to ensure API credentials cannot be intercepted.

For more detail you can refer the documentation at here

Saif
  • 2,873
  • 2
  • 12
  • 30
1

Error with Postman (but could apply to any)

I finally discovered that the issue was a final trailing slash to the Host I provided to PostMan.

To understand what url the website was based of after so much time waste I went to Settings -> General Settings: WordPress Address (URL) & Site Address (URL)

enter image description here

Hence I took the correct host from there.

In Post man then I used Aouth.1 making sure that is set to Request Boddy / Request URL.

enter image description here

Federico Baù
  • 6,013
  • 5
  • 30
  • 38
0

I was having the same issue. make sure url: 'http://example.com' matches exactly the one you have in wordpress general setting page. for example both start with www and have the exact protocol of https

Ali Kazemi
  • 606
  • 4
  • 12
0

The header must contain key "Content-Type": "application/json"

I am using the OAuthRequest, where it is coded by:

request.addHeader("Content-Type", "application/json");

It works for me fine even with HTTP.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
6-ka
  • 1
0

For my case, I found the issue was due to the URL used on Postman is using www., but under WordPress > General Settings on both WordPress Address (URL) and Site Address (URL) was set without www. So, it makes sense that the signature is invalid.

Rifki
  • 1
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 22 '21 at 06:43
-1

In the object you are passing through to the functions (var data) your key also need to be a string.

eg.

var data = { "first_name": "Joe" //and so on

}

Suluuboi
  • 69
  • 9