使用LinkedIn V2 API上传视频

时间:2019-04-30 14:32:57

标签: php api post linkedin linkedin-api

我正在尝试使用LinkedIn API V2上传视频,但无法将视频成功发布到我的LinkedIn个人帐户。 请帮忙。

从LinkedIn API返回以下响应:

  

SignatureDoesNotMatch   我们计算出的请求签名与您提供的签名不匹配。检查您的密钥和签名方法。

$person_id=LINKEDIN_ACCOUNT_ID;
$access_token= LINKEDIN_ACCESS_TOKEN;

$share_text='Video Upload and Share Text';
$author = "urn:li:person:".$person_id;

$r_url='https://api.linkedin.com/v2/assets?action=registerUpload';

$r_params = array(
    'registerUploadRequest'=>array(
        'recipes'=>array('urn:li:digitalmediaRecipe:feedshare-video'),                  
        'owner' => $author,
    )
);

$handle = curl_init();
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($handle, CURLOPT_URL, $r_url);
curl_setopt($handle, CURLOPT_VERBOSE, FALSE);
$header = array();
$header[] ='Authorization : Bearer '.$access_token;
$header[] = 'Content-Type: application/json; charset=UTF-8';

curl_setopt($handle, CURLOPT_HTTPHEADER, $header);
curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($r_params));
$json1 = curl_exec($handle);
$json1=json_decode($json1,true);

if($json1['value']['uploadMechanism']['com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest']['uploadUrl']){
    $target_url=$json1['value']['uploadMechanism']['com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest']['uploadUrl'];

    $return_header=$json1['value']['uploadMechanism']['com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest']['headers'];
    $parts = parse_url($target_url);
    parse_str($parts['query'], $query);

    $amz_signature=$query['X-Amz-Signature'];

    $target_header=array();

    $target_header[]='Host: video-uploads-prod.s3-accelerate.amazonaws.com';
    $target_header[]="Content-Type:".trim($return_header['Content-Type']);

    $target_header[]="x-amz-server-side-encryption:".trim($return_header['x-amz-server-side-encryption']);
    $target_header[]='x-amz-server-side-encryption-aws-kms-key-id:'.trim($return_header['x-amz-server-side-encryption-aws-kms-key-id']);


    $video_path = DIR_PATH_TO_VIDEO_FILE.'example_video.mp4';

    $post_data=array('file'=>$video_path);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_URL,$target_url);
    curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $target_header);

    curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($video_path));

    $json2=curl_exec ($ch);
    curl_close ($ch);

    $json2=json_decode($json2,true);

    $media_id=str_replace('urn:li:digitalmediaAsset:','', $json1['value']['asset']);

    $return_data=array();
    $check_url = 'https://api.linkedin.com/v2/assets/'.$media_id;

    $handle = curl_init();
    curl_setopt($handle, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($handle, CURLOPT_HEADER, FALSE);
    curl_setopt($handle, CURLOPT_URL, $check_url);
    $header = array();
    $header[] ='Authorization : Bearer '.$access_token;
    $header[] = 'Content-Type: application/json; charset=UTF-8';
    curl_setopt($handle, CURLOPT_HTTPHEADER,$header);
    $return_data= curl_exec($handle);
    $return_data= json_decode($return_data,true);


    $author = "urn:li:person:".$person_id;

    $post_url = 'https://api.linkedin.com/v2/ugcPosts';
    $media_data=array();
    $media_data[0]=array(
            'status'=>'READY',
            'description'=>array('text'=>'Official LinkedIn Blog'),
            'media'=>$media_id,
            'title'=>array('text'=>"Official LinkedIn Blog"),
        );

    $params = array(
        'author' => $author,
        'lifecycleState' => 'PUBLISHED',
        'specificContent' => array(
            'com.linkedin.ugc.ShareContent' => array(
                'shareCommentary' => array(
                    'text' => "Video media set in post",
                ),
                'shareMediaCategory' => 'VIDEO',
                'media'=>$media_data,
                'originalUrl'=>'https://www.google.com'
            )
        ),
        'visibility' => array(
            'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC'
        )
    );


    $handle = curl_init();
    curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($handle, CURLOPT_URL, $post_url);
    curl_setopt($handle, CURLOPT_VERBOSE, FALSE);
    curl_setopt($handle, CURLOPT_POSTFIELDS, json_encode($params));
    $header = array();
    $header[] ='Authorization : Bearer '.$access_token;
    $header[] = 'Content-Type: application/json; charset=UTF-8';
    $header[] = 'X-Restli-Protocol-Version:2.0.0';
    curl_setopt($handle, CURLOPT_HTTPHEADER, $header);
    $json3 = curl_exec($handle);
    $json3=json_decode($json3);

我需要成功将视频帖子上传到LinkedIn帐户,但我也无法从LinkedIn文档中了解这一点。我已经尝试了很多,但没有成功。 请成功通过V2上传视频的人帮助。

2 个答案:

答案 0 :(得分:1)

您好,linkedin还没有发布视频。您可以使用文章EP(“ shareMediaCategory”:“ ARTICLE”)将视频发送到linkedin

答案 1 :(得分:0)

我利用LinkedIn API from Zoonman进行客户发布请求,但这超出了问题的范围。

因为我无法使php curl函数正常工作,所以我正在使用命令行界面来执行请求,并且它可以正常工作!请参阅下面的代码。

但是。甚至很难上传。当我请求获取上传状态时,它仍然是“ WAITING_UPLOAD”。所以我认为@augustine jenin是正确的,尚不支持。 (2019年5月)

<?php
// first register upload
$data = [
	"registerUploadRequest" => [
		"recipes" => [
			"urn:li:digitalmediaRecipe:feedshare-video"
		],
		"owner" => "urn:li:organization:" . $liPageId,
		"serviceRelationships"=> [
			[
				"relationshipType"=> "OWNER",
				"identifier" => "urn:li:userGeneratedContent"
			]
		]
	]
];
    
$register = $client->post('assets?action=registerUpload', $data);

// get upload url and header
$uploadUrl = $register["value"]["uploadMechanism"]["com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest"]["uploadUrl"];
$headers = $register["value"]["uploadMechanism"]["com.linkedin.digitalmedia.uploading.MediaUploadHttpRequest"]["headers"];
    
$curlHeaders = "";
foreach($headers as $htype => $header) {
	$curlHeaders .= ' -H "' . $htype . ':' . $header . '"';
}

// go upload the image to the url
$filePath = "/path/to/your/file";

$command = '/usr/bin/curl -v';
$command .= $curlHeaders;
$command .= ' --upload-file \'' . $filePath . '\' \'' . $uploadUrl . '\'';

// try it yourself by running this on the command line
//echo $command;

shell_exec($command);

?>