1

I am having difficulty getting a share on 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. 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
}
Divyashree
  • 21
  • 1
  • 5

2 Answers2

4

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)

  2. For some reason, the format of the headers array that you defined is throwing an error. 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.

the.marolie
  • 1,032
  • 2
  • 7
  • 14
  • Thank you. But when I run with the modified code, I got the below error message. {"serviceErrorCode":65604,"message":"Empty oauth2 access token","status":401} – Divyashree Apr 03 '19 at 06:13
  • @Divyashree, mmm...I'm not getting any error. Could you please update the entire code which you are using right now. – the.marolie Apr 03 '19 at 06:45
  • @Divyashree...glad to know that you solved it. But I get "Multiple access token provided" error when i use that URL, that's why I changed it. – the.marolie Apr 03 '19 at 06:50
1

ISSUE SOLVED

Added the access_token both in URL and header. 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);
Divyashree
  • 21
  • 1
  • 5
  • But how do we get access_token? I am getting the following error. { "serviceErrorCode": 65604, "message": "Empty oauth2 access token", "status": 401 } – Shashank Shah Apr 17 '20 at 11:30