通过LinkedIn API V2创建图像共享不起作用 [英] Create an Image Share over LinkedIn API V2 not working

查看:94
本文介绍了通过LinkedIn API V2创建图像共享不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是api版本2.0,并且想要创建图像共享.

I use the api version 2.0 and want to create an image share.

Linkedin在这里描述您的图像二进制文件上传过程:

Linkedin describes your image binary file upload prozess here: 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状态.到目前为止一切顺利!

If you follow the instructions you will get a 400 HTTP error. I add the Content-Type in the header and get a 201 HTTP status with the X-RestLi-Id in the header. So far so good!

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

If I want to show my created post on linkedin (https://www.linkedin.com/in/me/detail/recent-activity/) I can't find the post with image.

如何解决?有人知道吗?

How to fix it? Anybody got an idea?

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

推荐答案

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

You need to change your request to PUT and keep default for 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简化上传:

Or use GuzzleHttp\Client to simplify the upload:

$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'),

    ]

这篇关于通过LinkedIn API V2创建图像共享不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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