【问题标题】:etsy-api-oauth1 PUT Requests return invalid signature when made from python, but not from Postmanetsy-api-oauth1 PUT 请求从 python 发出时返回无效签名,但不是来自 Postman
【发布时间】:2020-02-16 21:08:21
【问题描述】:

在向 ETSY REST API 发送 PUT 请求时,我一直在解决一个问题, 当我发出 PUT 请求时,我得到一个 signature invalid error。我已经尝试了在谷歌上找到的所有解决方案,但似乎没有一个有效。

如果我指定 auth 参数进入正文,则通过 POSTMAN 发送相同的请求是可行的。但是,当我尝试使用 python 的标准程序复制它时,我得到了相同的invalid signature error

即使我为邮递员的确切请求导出了 python 代码,在 python 中运行所述代码时,我也会收到签名无效错误。

这是我需要发送的json数据:

"products": [
      {
         "product_id":4262200422,
         "sku":"00100012",
         "property_values":[
            {
               "property_id":200,
               "property_name":"Primary color",
               "scale_id":null,
               "scale_name":null,
               "values":[
                  "Black"
               ],
               "value_ids":[
                  1
               ]
            }
         ],
         "offerings":[
            {
               "offering_id":4128359213,
               "price":{
                  "amount":200,
                  "divisor":100,
                  "currency_code":"GBP",
                  "currency_formatted_short":"\\u00a32.00",
                  "currency_formatted_long":"\\u00a32.00 GBP",
                  "currency_formatted_raw":"2.00"
               },
               "quantity":12,
               "is_enabled":1,
               "is_deleted":0
            }
         ],
         "is_deleted":0
      },
      {
         "product_id":4031391357,
         "sku":"00100013",
         "property_values":[
            {
               "property_id":200,
               "property_name":"Primary color",
               "scale_id":null,
               "scale_name":null,
               "values":[
                  "Bronze"
               ],
               "value_ids":[
                  1216
               ]
            }
         ],
         "offerings":[
            {
               "offering_id":4244423138,
               "price":{
                  "amount":300,
                  "divisor":100,
                  "currency_code":"GBP",
                  "currency_formatted_short":"\\u00a33.00",
                  "currency_formatted_long":"\\u00a33.00 GBP",
                  "currency_formatted_raw":"3.00"
               },
               "quantity":56,
               "is_enabled":1,
               "is_deleted":0
            }
         ],
         "is_deleted":0
      }
   ],
"price_on_property": [200],
"quantity_on_property": [200],
"sku_on_property": [200]

Etsy 使用 Oauth1,POST 和 GET 请求在 Python 中可以正常工作,但在 POSTMAN 中无法正常工作,返回无效签名错误。

也许他们正在做一些不同的事情导致这种行为?

我需要构建一个简单地获取产品数据、oauth1 详细信息并将其发送到 Etsy 的请求。 请让我知道我缺少什么。

【问题讨论】:

    标签: python python-3.x authentication oauth etsy


    【解决方案1】:

    我设法让它工作......原来你不应该在签名计算中包含实际的 oauth 参数。

    这是我用来发出 PUT 请求的代码 --- 类定义。

    class etsyClient:
    def __init__(self, consumer_key, consumer_secret, resource_owner_key, 
                                                      resource_owner_secret):
        self.consumer_key = consumer_key
        self.consumer_secret = consumer_secret
        self.resource_owner_key = resource_owner_key
        self.resource_owner_secret = resource_owner_secret
    
        self.oauthclient = self.regen()
    
    def regen(self, sig=oauth1.SIGNATURE_HMAC_SHA1):
        client = oauth1.Client(self.consumer_key,
                               client_secret=self.consumer_secret,
                               resource_owner_key=self.resource_owner_key,
                               resource_owner_secret=self.resource_owner_secret,
                               signature_method=sig)
        return client
    

    实际请求代码:

        uri, headers, body = self.client.regen(sig=oauth1.SIGNATURE_PLAINTEXT).sign(
            f"https://openapi.etsy.com/v2/listings/{self.product.listing.id}/inventory")
    
        r = requests.session()
        headers["Content-Type"] = "application/x-www-form-urlencoded"
        data = {
                "listing_id": self.product.listing.id,
                "products": json.dumps(products),
                'price_on_property': [200],
                'quantity_on_property': [200],
                'sku_on_property': [200]
        }
        a = r.put(uri, headers=headers, data=data)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-31
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 2021-08-17
      相关资源
      最近更新 更多