LinkedIn REST API - Internal Server Error

I am using the LinkedIn REST API to publish updates on the users timeline. Starting from a few days, I get an Internal server error response from LinkedIn, but the code worked earlier.

PHP:

 $postTitle = "hello"; $postDesc = "world "; $submitted-url = "http://example.com"; $submitted-image-url = "http://images.example.com/img/hello.jpg"; $comment = "foobar"; $postData = array('visibility' => array('code' => 'connections-only'), 'content' => array('title' => $postTitle,'description' => $postDesc,'submitted-url' => $postURL,'submitted-image-url' => $postImage), 'comment' => $postComment); $ch = curl_init('https://api.linkedin.com/v1/people/~/shares?oauth2_access_token='.$oauthToken.'&format=json' ); curl_setopt_array($ch, array( CURLOPT_POST => TRUE, CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json"), CURLOPT_POSTFIELDS => json_encode($postData) )); $response = curl_exec($ch); 

How to fix this error?

+6
source share
4 answers

Your code is invalid PHP (perhaps due to some changes that you made before publishing?); changing it to:

 $postTitle = "hello"; $postDesc = "world "; $postURL = "http://example.com"; $postImage = "http://images.example.com/img/hello.jpg"; $postComment = "foobar"; $oauthToken = "<token>"; $postData = array( 'visibility' => array('code' => 'connections-only'), 'content' => array( 'title' => $postTitle, 'description' => $postDesc, 'submitted-url' => $postURL, 'submitted-image-url' => $postImage ), 'comment' => $postComment ); $ch = curl_init('https://api.linkedin.com/v1/people/~/shares?oauth2_access_token='.$oauthToken.'&format=json'); curl_setopt_array($ch, array( CURLOPT_POST => TRUE, CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json"), CURLOPT_POSTFIELDS => json_encode($postData) )); $response = curl_exec($ch); 

works if only $oauthToken set to a valid token. Assuming your real code is correct, the only possible possibility is that your OAuth token has expired and you need to get a new one first. By adding CURLOPT_VERBOSE => TRUE to the cURL parameters, you will learn more about the error that LinkedIn returns.

+4
source

Instead, you can use the LinkedIn PHP SDK (provided by the community): https://github.com/Happyr/LinkedIn-API-client

+2
source

We recently encountered a similar problem with the Linkedin API. Finally, a fix emerged by changing the URL.

New URL: " https://api.linkedin.com/v1/people/~/shares "

Instead of specifying "oauth2_access_token" in the query line, add it to the header - specify:

"Authorization", "Media" + accessToken.

And finally, in the body parameter of the request, add your json / xml data to publish

+1
source

You must use the authentication token in the request headers.

This is working code. Give it a try.

 $postTitle = "hello"; $postDesc = "world "; $submitted-url = "http://example.com"; $submitted-image-url = "http://images.example.com/img/hello.jpg"; $comment = "foobar"; $oauthToken = "TokenHere"; $postData = array('visibility' => array('code' => 'connections-only'), 'content' => array('title' => $postTitle,'description' => $postDesc,'submitted-url' => $postURL,'submitted-image-url' => $postImage), 'comment' => $postComment); $ch = curl_init('https://api.linkedin.com/v1/people/~/shares?format=json'); curl_setopt_array($ch, array( CURLOPT_POST => TRUE, CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_HTTPHEADER => array('x-li-format: json', "Content-Type: application/json", "Bearer: ".$oauthToken.""), CURLOPT_POSTFIELDS => json_encode($postData) )); $response = curl_exec($ch); 
+1
source

Source: https://habr.com/ru/post/985294/


All Articles