我正在尝试从woocommerce获取数据,它需要OAuth 1.0授权。
尽管尝试了多种方式,尽管邮递员给了我200,但我仍然收到401错误“无效签名”。我相信问题出在hash_function
位,但我不知道如何诊断或解决问题。
这是我的代码:
componentDidMount() {
const key = 'xxxxxxx';
const timeStamp = Math.floor(new Date().getTime()/1000);
const nonce = crypto.randomBytes(16).toString('base64');
let base_string = 'GET&http%3A%2F%2Flocalhost%3A8888%2Fwp-json%2Fwc%2Fv2%2Fproducts%2F28%2Fvariations&oauth_consumer_key%3Dxxxxxx%26oauth_nonce%3DtnnUqiEHxgT4zSRaXnZRLA%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1533375490'
const oauth = OAuth({
consumer: {
key: 'xxxxxx',
secret: 'xxxxx',
signature_method: 'HMAC-SHA1',
hash_function(base_string, key) {
return crypto.createHmac('sha1', key).update(base_string).digest('base64');
}
},
});
const request_data = {
url: 'http://localhost:8888/wp-json/wc/v2/products/28/variations',
method: 'GET',
};
const token = {
key: 'xxxxxx',
secret: 'xxxxx',
};
fetch(request_data.url, {
method: request_data.method,
headers: oauth.toHeader(oauth.authorize(request_data, token))
})
.then(response => response.json())
.then(response => {
this.setState({
products: response
})
})
}