ciaranj / node-oauth

OAuth wrapper for node.js
MIT License
2.44k stars 660 forks source link

etsy oauth image upload #354

Open oguzhanbulut opened 3 years ago

oguzhanbulut commented 3 years ago

I am having a problem with this.

I have been experimenting for 3 days, but I cannot upload the image uploaded by postman with javascript.

I used such a code. ` var authHeader = oa.authHeader( "https://openapi.etsy.com/v2/listings/"+listingId+"/images", req.session.oauth.access_token, req.session.oauth.access_token_secret, );

console.log(authHeader);

var options = {
    "method": "POST",
    "url": "https://openapi.etsy.com/v2/listings/"+listingId+"/images",
    "headers": {
        "Authorization": authHeader,
    },
    formData: {
        "image": {
            "value": fs.createReadStream(path.join(__dirname, "images/"+image_path+".jpg")),
            "options": {
                "filename": path.join(__dirname, "images/"+image_path+".jpg"),
                "contentType": null
            }
        }
    }
};
request(options, function (error, response) {
    if (error) throw new Error(error);
    console.log(response.body);
});

`

I get this error.

OAuth oauth_consumer_key="cpuao0wzd2q3l152x30nzebp",oauth_nonce="R88aVNfFRhz17yA3nQYeX4ZE1PfG1Zhb",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1597230278",oauth_token="a02ee077a56d97527ade2ecd24423c",oauth_version="1.0A",oauth_ signature="iCBcTHEXb638wXabJPJ2RUQeeew%3D" oauth_problem=signature_invalid&debug_sbs=POST&https%3A%2F%2Fopenapi.etsy.com%2Fv2%2Flistings%2F839582160%2Fimages&oauth_consumer_key%3Dcpuao0wzd2q3l152x30nzebp%26oauth_nonce%3DR88aVNfFRhz17yA3nQYeX4ZE1PfG1Zhb%26oauth_signa ture_method%3DHMAC-SHA1%26oauth_timestamp%3D1597230278%26oauth_token%3Da02ee077a56d97527ade2ecd24423c%26oauth_version%3D1.0A

ngr900 commented 3 years ago

I have not been able to resolve this problem using node-oauth but I did manage to get the image upload working using request. It's a deprecated package but I needed something that worked, quickly. Here is the code:

function uploadListingImage(listingId, image) {
    const req = request.post({
      url: `https://openapi.etsy.com/v2/listings/${listingId}/images`,
      oauth: {
        consumer_key: etsyKeys.key,
        consumer_secret: etsyKeys.secret,
        token: etsyKeys.user_token,
        token_secret: etsyKeys.user_secret
      }
    }, (err, response, body) => {
      if (err) {
        // error
      }
      if (response.statusCode === 201) {
        // success
        console.log(body.results[0]);
      }
    });
    const form = req.form();
    form.append('image', image, {
      filename: 'cover-image.jpg',
      contentType: 'image/jpeg'
    });
  }

Where listingId is self-explanatory and image is a buffer of the image read using fs.readFile.

SteveMcArthur commented 3 years ago

I think the problem is that node-oauth puts the authorization parameters in the header, whilst the etsy api expects them to be in the post body. I tried to hack it to make it work but in the end did more or less the same thing as you. In my case I used oauth-1.0a to generate authorization content.