3

I am trying to upload an image to Etsy through the API. However, the example on their website is given in PHP which I don't know how to code. I only know how to work with Python. On their website it says:

Image uploads can be performed using a POST request with the Content-Type: multipart/form-dataheader, following RFC1867.

Their example which is in PHP

// You must define the constants OAUTH_CONSUMER_KEY and OAUTH_CONSUMER_SECRET
// You must also assign values to the variables $access_token, $access_token_secret,
// $listing_id and $filename, and $mimetype.
// Your image file is assumed to be in the same directory as this code.

$oauth = new OAuth(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET);

$oauth->enableDebug();
$oauth->setToken($access_token, $access_token_secret);

try {
    $source_file = dirname(realpath(__FILE__)) ."/$filename";

    $url = "https://openapi.etsy.com/v2/listings/".$listing_id."/images";
    $params = array('@image' => '@'.$source_file.';type='.$mimetype);

    $oauth->fetch($url, $params, OAUTH_HTTP_METHOD_POST);

    $json = $oauth->getLastResponse();
    print_r(json_decode($json, true));

} catch (OAuthException $e) {
    // You may want to recover gracefully here...
    print $oauth->getLastResponse()."\n";
    print_r($oauth->debugInfo);
    die($e->getMessage());
}

In API doc it says

    HTTP Method =   POST
    URI  =  /listings/:listing_id/images

This is what I have so far in Python:

client_secret=client_secret,resource_owner_key=resource_owner_key,resource_owner_secret=resource_owner_secret)

url = 'https://openapi.etsy.com/v2/listings'
payload = {'listing_id':'2343432434', 'images': :('test1.jpg', open('test1.jpg', 'rb'), 'image/jpeg')}
result = etsy.post(uri, params=payload)

When I run this code I get an 403 error. How can I fix my code so that it functions correctly? I have looked at other examples and just can't seem to make it work.

halfer
  • 19,824
  • 17
  • 99
  • 186
ben olsen
  • 663
  • 1
  • 14
  • 27
  • I used to use the python oauthlib package at https://pypi.python.org/pypi/oauthlib for similar tasks, it handles all the request signing logic, might be worth a look, I don't have an Etsy account to try it with direct though. – AS Mackay Jul 04 '17 at 14:34

0 Answers0