通过LinkedIn API V2创建图像共享不起作用

时间:2019-03-05 09:03:00

标签: php linkedin linkedin-api

我使用的是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, and will only consist of 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);

1 个答案:

答案 0 :(得分:0)

您需要将请求更改为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'),

    ]