Search code examples
pythonimageoauthmultipartetsy

Etsy API python image upload


does anyone use python for adding listings to Etsy using API? I am using requests_oauthlib, but keep getting error when want to upload image to listing. Other functions, like listing creation etc. are working. Code of image upload:

self.oauth = OAuth1(self.api_key,
                    client_secret=self.api_secret,
                    resource_owner_key=self.oauth_token,
                    resource_owner_secret=self.oauth_token_secret)

def upload_picture(self):
    url = "https://openapi.etsy.com/v2/listings/{listing_id}/images"
    headers = {
        "Content-Type": "multipart/form-data",
    }
    request = {
        'image': ('img8.jpg', open('./img8.jpg', 'rb'), 'image/jpeg'),
    }

    r = requests.post(url=url, params=request, auth=self.oauth, headers=headers)

    print(r.content)

error:

oauth_problem=signature_invalid

Thanks in advance!


Solution

  • I wasn't able to do it like that. I'm not sure why.
    But below worked for me:

    etsy = OAuth1Session(
        ApplicationKey,
        ApplicationSecret,
        token, token_secret
    )
    
    url = "https://openapi.etsy.com/v2/listings/{listing_id}/images"
    request = { 'image': open('./img8.jpg', 'rb') }
    
    response = etsy.post(url, files = request)
    

    I'm new to both OAuth & Etsy API