首页 > 解决方案 > 为什么在将照片上传到 LinkedIn API 时收到 CLIENT_ERROR?

问题描述

我正在使用 LinkedIn v2 api 制作图像共享。根据LinkedIn 文档,分为三个步骤。

  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);

标签: linkedinlinkedin-apiphp-curl

解决方案


将标题添加到您的代码中Content-Type:application/octet-stream


推荐阅读