将照片上传到LinkedIn API时为什么会出现CLIENT_ERROR?

时间:2019-03-18 20:36:59

标签: linkedin linkedin-api php-curl

我正在使用LinkedIn v2 api进行图像共享。根据{{​​3}},该过程分为三个步骤。

  1. 注册要上传的图片。
  2. 将您的图片上传到LinkedIn。
  3. 创建图像共享。

完成第2步后,我使用/ v2 / assets / {asset-id}检查上传状态,并获得“ CLIENT_ERROR”。我不知道这意味着什么,也没有在LinkedIn文档或在线中找到太多有关它的信息。按照LinkedIn的要求,它可能与上传二进制图像文件有关,但据我所知,我正在上传一个。

编辑:我用来上传图像的php-curl在下面。 $ uploadUrl是从映像寄存器获得的(步骤1)。

    $data = [
        'file' => curl_file_create($file, $mimeType)//;
    ];

    ob_start();
    $out = fopen('php://output', 'w');
    $ch = curl_init($uploadUrl);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_STDERR, $out);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch,CURLOPT_USERAGENT,'curl/7.35.0');

    $authorizationHeader = trim("Authorization: Bearer $accessToken");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorizationHeader,"Content-Type: {$mimeType}","X-Restli-Protocol-Version: 2.0.0"));

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
    curl_setopt($ch, CURLOPT_UPLOAD, '1L');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $response = curl_exec($ch);

    fclose($out);
    $debug = ob_get_clean();
    print_r($debug);

    $resp_obj = json_decode($response);
    print_r($response);
    curl_close($ch);

资产API正在返回:

> GET /v2/assets/{redacted} HTTP/1.1
User-Agent: curl/7.35.0
Host: api.linkedin.com
Accept: */*
Authorization: Bearer {redacted}

< HTTP/1.1 200 OK
< X-LI-ResponseOrigin: RGW
< Content-Type: application/json
< X-RestLi-Protocol-Version: 1.0.0
< Content-Length: 319
< Date: Wed, 20 Mar 2019 14:09:18 GMT
< X-Li-Fabric: prod-ltx1
< Connection: keep-alive
< X-Li-Pop: prod-edc2-nkernB
< X-LI-Proto: http/1.1
< X-LI-UUID: {redacted}
< Set-Cookie: {redacted}
< X-LI-Route-Key: {redacted}

响应对象是:

(
[serviceRelationships] => Array
    (
        [0] => stdClass Object
            (
                [identifier] => urn:li:userGeneratedContent
                [relationshipType] => OWNER
            )

    )

[recipes] => Array
    (
        [0] => stdClass Object
            (
                [recipe] => urn:li:digitalmediaRecipe:feedshare-image
                [status] => CLIENT_ERROR
            )

    )

[mediaTypeFamily] => STILLIMAGE
[created] => 1553090957146
[lastModified] => 1553090958505
[id] => {redacted}
[status] => ALLOWED
)

更新:当我使用命令行curl上传图像时,效果很好:

curl -i --upload-file {file} --header "Authorization: Bearer {auth}" {url}

更新:解决方案:

使用file_get_contents :curl_setopt($ ch,CURLOPT_POSTFIELDS,file_get_contents({path-to-your-image)); **

    $ch = curl_init($uploadUrl);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch,CURLOPT_USERAGENT,'curl/7.35.0');
    $authorizationHeader = trim("Authorization: Bearer $accessToken");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorizationHeader,"Content-Type: {$mimeType}","X-Restli-Protocol-Version: 2.0.0"));
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);

    curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents({path-to-your-image));
    $response = curl_exec($ch);

3 个答案:

答案 0 :(得分:0)

这是:curl_setopt($ ch,CURLOPT_POSTFIELDS,file_get_contents({path-to-your-image));

    $ch = curl_init($uploadUrl);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch,CURLOPT_USERAGENT,'curl/7.35.0');
    $authorizationHeader = trim("Authorization: Bearer $accessToken");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorizationHeader,"Content-Type: {$mimeType}","X-Restli-Protocol-Version: 2.0.0"));
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);

    curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents({path-to-your-image));
    $response = curl_exec($ch);

答案 1 :(得分:0)

添加标题Content-Type:应用程序/八位字节流

答案 2 :(得分:-1)

Authorization': Bearer ${ access_token },

'X-Restli-Protocol-Version': '2.0.0',

'内容类型':'图像/jpg'

body 只是图像文件内容或 BLOB

方法:POST 或 PUT...我想其中一个适用于某些人,第二个适用于其他人