Etsy API图片上传错误

时间:2018-03-11 15:44:02

标签: php oauth http-status-code-400

我有一个oauth连接 - 它适用于所有其他请求,但有一个: 上传图片列表。 这是代码:

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

但是,回应是:

"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"

我错过了什么?该文件未上传,并收到上述错误。

2 个答案:

答案 0 :(得分:1)

您是否尝试过使用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());
}

我不熟悉PHP并且从C#获得相同的错误消息。

我问,因为,如果你使用他们的PHP脚本得到相同的响应(但显然你的值),那么你可以向他们提出支持请求(Etsy Developer Support)并指出“它不起作用”。然后Etsy支持应该在他们的最后尝试请求,如果它有效,你知道你做错了(回到我知道的方方面面,但至少你知道它应该有用......)。但是,如果他们回来“啊,它已经破碎了”......

我的问题是它曾经工作过,我有一个来自调用的示例响应,但现在它没有(我正在使用RestSharp进行经过身份验证的调用)。

答案 1 :(得分:1)

经过多次尝试,我发现了使用curl的解决方法。我认为我应该分享它,以便有相同问题的任何人都可以使用它。

$oauth = new OAuth($api_key, $api_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
$oauth->setToken($access_token, $access_token_secret);

$url = "https://openapi.etsy.com/v2/listings/864452611/images";
$method = "POST";
$image_path = "/path/to/image.jpg";

$file = new \CURLFile($image_path, mime_content_type($image_path), basename($image_path));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => $file));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'method' => $method,
    'header' => 'Content-Type: multipart/form-data; boundary=' . microtime(true),
    'header' => 'Authorization: ' . $oauth->getRequestHeader($method, $url)
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);
print_r(json_decode($server_output, true));

curl_close ($ch);