【问题标题】:Create an Image Share over LinkedIn API V2 not working通过 LinkedIn API V2 创建图像共享不起作用
【发布时间】:2019-03-05 09:03:00
【问题描述】:

我用的是api 2.0版本,想创建一个图片分享。

LinkedIn 在这里描述了您的图片二进制文件上传过程: https://docs.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/share-on-linkedin?context=linkedin/consumer/context#create-a-text-share

如果您按照说明进行操作,您将收到 400 HTTP 错误。 我在标头中添加 Content-Type 并在标头中使用 X-RestLi-Id 获得 201 HTTP 状态。到目前为止一切顺利!

如果我想在 LinkedIn (https://www.linkedin.com/in/me/detail/recent-activity/) 上显示我创建的帖子,我找不到带有图片的帖子。

如何解决?有人有想法吗?

PHP 代码:

$imageRequestData = array(
    "registerUploadRequest" => array(
        "recipes" => array(
            "urn:li:digitalmediaRecipe:feedshare-image"
        ),
        "owner" => $urn, // Person URN === urn:li:person:XXXX
        "serviceRelationships" => array(
            array(
                "relationshipType" => "OWNER",
                "identifier" => "urn:li:userGeneratedContent"
            )
        )
    )
);
$image_request = $this->post('v2/assets?action=registerUpload', $imageRequestData);

$headers = array();
$headers[] = 'Authorization: Bearer ' . $this->accessToken;
$headers[] = 'X-Restli-Protocol-Version: 2.0.0';
$headers[] = 'Content-Type: ' . mime_content_type($image_path);  //ex. image/png
$ch = curl_init();
$options = array(
    CURLOPT_HEADER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $image_request['message']['value']['uploadMechanism']
      ['com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest']['uploadUrl'],
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_POST => true,
    CURLOPT_CONNECTTIMEOUT => $this->connectTimeout,
    CURLOPT_TIMEOUT => $this->timeout,
    CURLOPT_POSTFIELDS => array("upload-file" => new CURLFile($image_path))
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);


$content = array(
    'author' => $urn, // Person URN === urn:li:person:XXX
    'lifecycleState' => 'PUBLISHED',
    'specificContent' => array(
        "com.linkedin.ugc.ShareContent" => array(
            'shareCommentary' => array(
                "text" => $comment,
            ),
            'shareMediaCategory' => 'IMAGE',
                // NONE - The share does not contain any media, only text.
                // ARTICLE - The share contains a URL.
                // IMAGE - The Share contains an image.
            'media' => array(
                    "status" => "READY",
                    "media" => $image_request['message']['value']['asset']
                )
        )
    ),
    "visibility" => array(
        "com.linkedin.ugc.MemberNetworkVisibility" => "PUBLIC"
    )
);


$postfields = json_encode($content);
$headers = array();
$headers[] = 'x-li-format: json';
$headers[] = 'Authorization: Bearer ' . $this->accessToken;
$headers[] = 'Content-Type: application/json';
$headers[] = 'Content-Length: ' . strlen($postfields);
$headers[] = 'X-Restli-Protocol-Version: 2.0.0';
$ch = curl_init();
$options = array(
    CURLOPT_HEADER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => 'https://api.linkedin.com/v2/ugcPosts',
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_POST => true,
    CURLOPT_CONNECTTIMEOUT => $this->connectTimeout,
    CURLOPT_TIMEOUT => $this->timeout,
    CURLOPT_POSTFIELDS => $postfields
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);

【问题讨论】:

    标签: linkedin linkedin-api


    【解决方案1】:

    您需要将您的请求更改为 PUT 并保持 SSL VerifyPeer 的默认值:

    $headers = array();
    $headers[] = 'Authorization: Bearer ' . $this->accessToken;
    $headers[] = 'X-Restli-Protocol-Version: 2.0.0';
    $headers[] = 'Content-Type: multipart/form-data';
    $ch = curl_init();
    $options = array(
        CURLOPT_HEADER => true,
        CURLOPT_CUSTOMREQUEST => 'PUT', //need to set custom request to PUT instead of post
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_URL => $image_request['message']['value']['uploadMechanism']
            ['com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest']['uploadUrl'],
        CURLOPT_HTTPHEADER => $headers,
        // CURLOPT_SSL_VERIFYPEER => false, //keep default options
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_POST => true,
        CURLOPT_CONNECTTIMEOUT => $this->connectTimeout,
        CURLOPT_TIMEOUT => $this->timeout,
        CURLOPT_POSTFIELDS => array("upload-file" => new CURLFile($image_path))
    );
    curl_setopt_array($ch, $options);
    $response = curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    

    或者使用 GuzzleHttp\Client 来简化上传:

    $client =new \GuzzleHttp\Client();
    $client->request('PUT',
        $image_request['message']['value']['uploadMechanism'] // ↵
            ['com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest']['uploadUrl'],
        [
            'headers' => [
                'Authorization' => 'Bearer ' . $this->accessToken
            ],
            'body' => fopen($image_path, 'r'),
        ]
    

    【讨论】:

    • curl 方法似乎对我不起作用,但 Guzzle 方法可以,不幸的是我不能依靠 guzzle 来做到这一点
    • 麻烦了好久。你的解决方案让我摆脱了这个麻烦,非常感谢@Nikola Kirincic
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 2019-08-31
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    相关资源
    最近更新 更多