Granga / etsy-ts

Etsy API wrapper written in typescript
https://www.npmjs.com/package/etsy-ts
MIT License
37 stars 5 forks source link

There is no way to use updateInventory #10

Closed haoxiaogang0919 closed 3 years ago

haoxiaogang0919 commented 3 years ago

oauth_problem=signature_invalid&debug_sbs=PUT&https%3A%2F%2Fopenapi.etsy.com%2Fv2%2Flistings%2F964345818%2Finventory&oauth_consumer_key%3Dfxpjow81qxm1ullsdvi0item%26oauth_nonce%3DjBL2EoZXHQyHTaSBC8VxIYDmjcpNWmqG%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1615410408%26oauth_token%3Db203a290d6696ba022d55579138e3a%26oauth_version%3D1.0%26products%3D%255B%257B%2522property_values%2522%253A%255B%257B%2522property_id%2522%253A513%252C%2522ott_value_qualifier%2522%253A1%252C%2522property_name%2522%253A%2522Bracelet%2520length%2522%252C%2522scale_id%2522%253Anull%252C%2522scale_name%2522%253Anull%252C%2522value_id%2522%253A7%252C%2522values%2522%253A%255B%252216%2520cm%2522%255D%257D%255D%252C%2522offerings%2522%253A%255B%257B%2522price%2522%253A99%252C%2522quantity%2522%253A100%257D%255D%257D%255D

haoxiaogang0919 commented 3 years ago
{
  validateStatus: [Function: validateStatus],
  url: '/listings/964345818/inventory',
  method: 'PUT',
  data: {
    products: '"[{"property_values":[{"property_id":513,"ott_value_qualifier":1,"property_name":"Bracelet ' +
      'length","value_id":3,"values":["16 ' +
      'cm"]}],"offerings":[{"price":99,"quantity":100}]}]"'
  }
}
cbanfiel commented 3 years ago

Same issue. Even when not using this library I was having the same error. If anyone has a working example that would be greatly appreciated

Granga commented 3 years ago

Hey guys. Although Etsy OAuth documentation suggests using HMAC-SHA1 as signature method, I've noticed it does not always work. What did work more consistently though, was setting the signature method to PLAINTEXT. Let me know if you think such change has negative impact on the security.

After updating to v3.2.3 you can use the following approach to update inventory:

// First get inventory
let inventory = (await client.ListingInventory.getInventory({
    listing_id: listingId,
    write_missing_inventory: 1 as any
}, {token})).data.results as any as IListingInventory;

// We must unescape strings, otherwise Etsy won't accept them back
let normalizedProducts = inventory.products.map(product => ({
    ...product,
    property_values: product.property_values.map(propertyValue => ({
        ...propertyValue,
        values: propertyValue.values.map(value => unescape(value))
    }))
}));

// For example, we increase product price by 0.01
normalizedProducts.forEach(product => {
    let price = product.offerings[0].price;
    product.offerings[0].price = (price.amount / price.divisor) + 0.01;
});

// Send the updated inventory back to Etsy
inventory = (await client.ListingInventory.updateInventory({
    ...inventory,
    listing_id: listingId,
    products: JSON.stringify(normalizedProducts)
}, {
    token: token
})).data.results as any as IListingInventory;