简体   繁体   English

如何使用 REST-API v2 在 LinkedIn 上分享?

[英]How to share on LinkedIn using REST-API v2?

I am having difficulty getting a share on linkedin.我在获得 Linkedin 上的份额时遇到了困难。 I am trying to post a LinkedIn share via linkedin api v2 and everytime I make the post request I get a request timed out (status 504) answer from the server.我正在尝试通过linkedin api v2发布LinkedIn共享,每次我发出发布请求时,我都会收到来自服务器的请求超时(状态504)答案。 Here is my code :这是我的代码:

$url = https://api.linkedin.com/v2/ugcPosts?oauth2_access_token=".$row[0]['accesstoken'];
$fields = '{
    "author": "urn:li:person:XXX",
    "lifecycleState": "PUBLISHED",
    "specificContent": {
        "com.linkedin.ugc.ShareContent": {
            "shareCommentary": {
                "text": "Hello World! This is my first Share on LinkedIn!"
            },
            "shareMediaCategory": "NONE"
        }
    },
    "visibility": {
        "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
    }
}';

$headers = array(
 'Content-Type' => 'application/json',
 'X-Restli-Protocol-Version' => '2.0.0'
 'Authorization' => 'Bearer'. $token);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($curl);

And here is the error message :
{
  "message": "Request timed out",
  "status": 504
}

Try the code below:试试下面的代码:

$url = "https://api.linkedin.com/v2/ugcPosts";

$headers = array('Content-Type: application/json','X-Restli-Protocol-Version: 2.0.0','x-li-format: json','Authorization: Bearer '.$token);

$fields = '{
    "author": "urn:li:person:*Person URN ID*",
    "lifecycleState": "PUBLISHED",
    "specificContent": {
        "com.linkedin.ugc.ShareContent": {
            "shareCommentary": {
                "text": "Hello World! This is my first Share on LinkedIn!"
            },
            "shareMediaCategory": "NONE"
        }
    },
    "visibility": {
        "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC"
    }
}';


$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
$httpCode = curl_getinfo($curl , CURLINFO_HTTP_CODE); // this results 0 every time
$response = curl_exec($curl);

if ($response === false) 
    $response = curl_error($curl);

echo stripslashes($response);

curl_close($curl);
  1. The correct API call URL is - https://api.linkedin.com/v2/ugcPosts (You don't have to include ?oauth2_access_token= in the URL)正确的 API 调用 URL 是 - https://api.linkedin.com/v2/ugcPosts (您不必在 URL 中包含?oauth2_access_token=

  2. For some reason, the format of the headers array that you defined is throwing an error.出于某种原因,您定义的 headers 数组的格式引发了错误。 So I have modified it.所以我已经修改了它。

  3. The Person URN ID has to be replaced by the URN ID of the user which has to be generated by another API call - see URN ID API for more details on how to achieve that. Person URN ID必须替换为用户的 URN ID,该 ID 必须由另一个 API 调用生成 - 有关如何实现的更多详细信息,请参阅URN ID API

ISSUE SOLVED问题已解决

Added the access_token both in URL and header.在 URL 和 header 中添加了 access_token。 Now the code is running.现在代码正在运行。 Other than that everything remains the same.除此之外,一切都保持不变。

$url      = "https://api.linkedin.com/v2/ugcPosts?oauth2_access_token=".$this->accesstoken;

$headers = array('Content-Type:application/json','X-Restli-Protocol-Version:2.0.0','x-li-format: json','Authorization:    Bearer'.$this->accesstoken);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM