Etsy API Image upload error

孤街浪徒 提交于 2020-01-06 03:37:24

问题


I have an oauth connection - it works well with all other requests, but one: upload image for listings. Here is the code:

$connection = $this->getEtsyConnection();

$imageApiUrl = 'https://openapi.etsy.com/v2/listings/'.$listingId.'/images';
$mimeType = mime_content_type($source_file);
$filesize = filesize($source_file);

$params = ['@image' => ''.$source_file.';type='.$mimeType, 'rank'=>1, 'overwrite'=>true, 'listing_id'=>intval($listingId) ];
$header = [
            'Content-Type' => 'multipart/form-data',
          ];
try {
    if ( file_exists( $source_file ) ) {                                      
        $connection->fetch($imageApiUrl, $params, OAUTH_HTTP_METHOD_POST, $header);
        $json = $connection->getLastResponse();
        print_r( json_decode($json, true) );
    }                            
} catch (\OAuthException $e) {
    $json = $connection->getLastResponse();
    print_r( $json );
    print_r( $params );
    print_r( $header );
    print_r( $connection->debugInfo );
    print_r( $e->getMessage() );
}

But, the response is:

"headers_recv" => """
    HTTP/1.1 400 Bad Request\r\n
    Server: Apache\r\n
    Set-Cookie: uaid=uaid%3D02SXR92A2MZuWR1R4gUobQcxhvYR%26_now%3D1520781833%26_slt%3D2KFBr_F0%26_kid%3D1%26_ver%3D1%26_mac%3Dym0kPXlXkm33j2_65CRxbmbOVWQ5Cb7aM1aSZIWzihw.; expires=Thu, 11-Apr-2019 07:42:13 GMT; Max-Age=34186700; path=/; domain=.etsy.com; secure; HttpOnly\r\n
    X-Etsy-Request-Uuid: EurHmFgGFL9b1gGn4HleCGHmwRd3\r\n
    X-Error-Detail: The request body is too large\r\n
    Cache-Control: private\r\n
    Set-Cookie: zuaid=uaid%3D02SXR92A2MZuWR1R4gUobQcxhvYR%26_now%3D1520781833%26_slt%3DIpYGzeIk%26_kid%3D1%26_ver%3D1%26_mac%3DwMLVY4w5yOPNJ9uLZMSaJIbmYA1rbvw7eOoS25FRu30.; expires=Tue, 10-Apr-2018 15:23:53 GMT; Max-Age=2592000; path=/; domain=.etsy.com; secure; HttpOnly\r\n
    Set-Cookie: user_prefs=kJV1qV14EKx95Oq3MxB4K75uceRjZACCqKVenDA6Oq80J0eHZCKWAQA.; expires=Mon, 11-Mar-2019 15:23:53 GMT; Max-Age=31536000; path=/; domain=.etsy.com\r\n
    Content-Type: text/plain;charset=UTF-8\r\n
    Content-Length: 29\r\n
    Accept-Ranges: bytes\r\n
    Date: Sun, 11 Mar 2018 15:23:53 GMT\r\n
    Via: 1.1 varnish\r\n
    Connection: close\r\n
    X-Served-By: cache-hhn1532-HHN\r\n
    X-Cache: MISS\r\n
    X-Cache-Hits: 0\r\n
    X-Timer: S1520781834.592519,VS0,VE351
    "
"body_recv" => "The request body is too large"

What am I missing? The file is not uploaded, and the error above is received.


回答1:


Have you tried using their PHP script:

// 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());
}

I'm not familiar with PHP and am getting the same error message but from C#.

I ask because, if you get the same response using their PHP script (but with your values obviously) then you can raise a support request with them (Etsy Developer Support ) and point out that "it doesn't work". Etsy Support should then try the request at their end and if it works, you know you're doing something wrong (back to square one I know, but at least you know it should work...). But if they come back with "Ah, it's broken"...

My problem is that it used to work, and I have an example response from the call, but now it doesn't (I'm using RestSharp to make authenticated calls).



来源:https://stackoverflow.com/questions/49221840/etsy-api-image-upload-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!