Woocommerce REST API状态:401 - 无效签名 - 提供的签名不匹配

时间:2017-12-09 15:34:44

标签: wordpress woocommerce woocommerce-rest-api

您好我使用的是WooCommerce API - Node.js客户端https://www.npmjs.com/package/woocommerce-api

我正在尝试创建一个需要向服务器发出POST请求的客户。 以下是初始化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'
});

这是创建客户的代码:

  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);
});

但我不断收到服务器的以下回复。

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

然而,对服务器上的工作的任何GET请求例如:我可以获得产品列表。

4 个答案:

答案 0 :(得分:1)

由于错误是不言自明的。这显然是身份验证问题。在文档中明确提到了

通过HTTPS

您可以通过提供API使用者密钥作为用户名和API使用者秘密作为密码来使用HTTP基本认证。

通过HTTP

您必须使用OAuth 1.0a“单腿”身份验证,以确保API凭据不会被截获。

有关更多详细信息,请参见here

中的文档。

答案 1 :(得分:0)

在要传递给功能(变量数据)的对象中,键也必须是字符串。

例如

var data = { “ first_name”:“乔” //等等

}

答案 2 :(得分:0)

我遇到了同样的问题。确保url: 'http://example.com'与您在wordpress常规设置页面中的完全匹配。例如,两者均以www开头,并具有确切的协议https

答案 3 :(得分:0)

标头必须包含键“ Content-Type”:“ application / json”

我正在使用OAuthRequest,它的编码方式是:

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

即使使用HTTP,它也可以正常工作。

相关问题