The Etsy API requires the oauth parameters to be included in the post body and not the headers. The documentation is not very clear on that but sort of implies it (https://www.etsy.com/developers/documentation/getting_started/oauth#section_authorized_put_post_requests)
So your request call should look like this:
request({
url: request_data.url,
method: request_data.method,
form: oauth.authorize(request_data, token)
},
function (error, response, body) {
console.log(response.body)
});
A good tool for debugging these sort of issues is Postman (https://www.postman.com/). It's how I was able to track down this issue.
Edit:
If the request is working in Postman but you still can't get it to work in your code then you can break the process down a bit. Build the oauth paramaters manually by calling the nonce, timestamp and signature methods on the oauth object.
let oauth_data = {
oauth_consumer_key: api_key,
oauth_nonce: oauth.getNonce(),
oauth_signature_method: "HMAC-SHA1",
oauth_timestamp: oauth.getTimeStamp(),
oauth_version: "1.0",
oauth_token: access_token
};
//now generate the signature using the request data and the oauth object
//you've just created above
let signature = oauth.getSignature(request_data,token_secret,oauth_data);
//now add the signature to the oauth_data paramater object
oauth_data.oauth_signature = signature;
//merge two objects into one
let formData = Object.assign({},oauth_data,request_data.params);
request({
url: request_data.url,
method: request_data.method,
form: formData
},
function (error, response, body) {
console.log(response.body)
});
I found in my code that I had misnamed the consumer.secret variable when initiating the oauth object. So the consumer secret was null and therefore the signature was coming out wrong.
Also, try out a more simpler PUT call just to test that your basic PUT request works. I was doing updateShop
to update the shop title (and you could actually update it to the existing title, thus not changing anything), which only needs your shop id and title string.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…