将照片上传到LinkedIn API时为什么会出现CLIENT_ERROR? [英] Why am I getting CLIENT_ERROR when uploading a photo to LinkedIn API?

查看:61
本文介绍了将照片上传到LinkedIn API时为什么会出现CLIENT_ERROR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LinkedIn v2 api进行图像共享.根据 LinkedIn文档.

I'm working on making an Image Share with the LinkedIn v2 api. There are three steps, according to the LinkedIn docs.

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

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

After I complete step 2, I check the status of the upload with /v2/assets/{asset-id} and get a "CLIENT_ERROR". I have no idea what this means and haven't found much about it in the LinkedIn docs or online. It may have something to do with uploading a binary image file as LinkedIn asks, but as far as I know I am uploading one.

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

The php-curl I'm using to upload the image is below. The $uploadUrl is obtained from the image register (step 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上传图像时,效果很好:

Update: it works fine when I upload the image using command line 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)); **

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

推荐答案

将标头作为 Content-Type:application/octet-stream

这篇关于将照片上传到LinkedIn API时为什么会出现CLIENT_ERROR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆