使用 LinkedIn V2 API 上传视频 [英] Upload video using LinkedIn V2 API

查看:34
本文介绍了使用 LinkedIn V2 API 上传视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to upload a video using LinkedIn API V2 but I am unable to post successfully video to my LinkedIn Individual Account. Please help.

从 LinkedIn API 返回以下响应:

Returning Below Response from LinkedIn API:

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

SignatureDoesNotMatch The request signature we calculated does not match the signature you provided. Check your key and signing method.

$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 视频的人帮忙.

I need to upload video post to LinkedIn Account successfully but I am unable to understand that from LinkedIn documentation too. I have tried so much but not succeed. Please someone who has successfully uploaded a video with V2 then please help.

推荐答案

我使用了 Zoonman 的 LinkedIn API 来执行客户端发布请求,但这超出了问题的范围.

I make use of the LinkedIn API from Zoonman to do the client post request, but this is out of the scope of the question.

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

Because i could not get the php curl functions to work properly, i am using the command line interface to do the request, and it works! See my code below.

但是.甚至艰难的上传作品.当我请求获取上传状态时,它仍然是WAITING_UPLOAD".所以我认为@augustine jenin 是对的,目前尚不支持.(2019 年 5 月)

BUT. even tough the upload works. When i do a request to get the status of the upload, it is still "WAITING_UPLOAD". So i think @augustine jenin is right, that it is not supported yet. (may 2019)

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

?>

这篇关于使用 LinkedIn V2 API 上传视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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