我正在尝试将一些产品从一个woocommerce商店登录到控制台,但我得到了这个输出:
{"code":"woocommerce_rest_authentication_error","message":"Invalid signature
- provided signature does not match.","data":{"status":401}}
当我通过postman生成此GET请求时,它可以正常工作,我将所有产品作为响应,但我需要通过我自己的api生成这个,但显然它不起作用。
POSTMAN GET请求:
本地主机/ WordPress的/可湿性粉剂JSON / WC / V2 /产品oauth_consumer_key = ck_040af47e7be12ed150bc39685cf3342cb7b202ca&安培; oauth_signature_method = HMAC-SHA1&安培; oauth_timestamp = 1497466469&安培; oauth_nonce = 6gPCbr4bcCR&安培; oauth_version = 1.0&安培; oauth_signature = 6JitOTSRbyTP6Wy4sE3WkX0Tq6I%3D
var request = require('request');
var OAuth = require('oauth-1.0a');
var crypto = require('crypto');
var express = require('express');
var app = express();
var port = process.env.PORT || 8000;
app.get('/products', function (req, res) {
res.send(products);
});
app.listen(port, function () {
console.log("Server running on PORT: " + port);
});
function hash_function_sha1(base_string, key) {
return crypto.createHmac('sha1',
key).update(base_string).digest('base64');
}
var oauth = OAuth({
consumer: {
key: 'ck_040af47e7be12ed150bc39685cf3342cb7b202ca',
secret: 'cs_516069533d8b50f1e572b5662f79c56cfa992142'
},
signature_method: 'HMAC-SHA1',
nonce_length: 32,
version: '1.0',
hash_function: hash_function_sha1
});
var request_data = {
url: 'http://localhost/wordpress/wp-json/wc/v2/products',
method: 'GET',
data: {
status: 'Hello Ladies + Gentlemen, a signed OAuth request!'
}
};
var token = {
key: 'ck_040af47e7be12ed150bc39685cf3342cb7b202ca',
secret: 'cs_516069533d8b50f1e572b5662f79c56cfa992142'
};
var products = request({
url: 'http://localhost/wordpress/wp-json/wc/v2/products',
method: 'GET',
form: request_data.data,
headers: oauth.toHeader(oauth.authorize(request_data, token))
}, function (error, response, body) {
console.log(body);
});