LinkedIn REST API - Internal server error
Asked Answered
W

4

6

I'm using the LinkedIn REST API to post updates to a users timeline. Since a few days I get an Internal server error response from LinkedIn but the code worked before.

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 that error?

Winna answered 16/4, 2015 at 18:0 Comment(0)
S
4

Your code is invalid PHP (perhaps because of some edits you made before posting?); modifying 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 is set to a valid token. Assuming your real code is correct the only possiblity left is that your OAuth token has expired and you need to obtain a new one first. By adding CURLOPT_VERBOSE => TRUE to the cURL options you would find out more about the error that LinkedIn returns.

Specimen answered 19/4, 2015 at 14:46 Comment(1)
Just tried your code with a valid token and got the same "internal service error": response :{ "errorCode": 0, "message": "Internal service error", "requestId": "J6PAQ9", "status": 500, "timestamp": 1429742 } Any ideas?Winna
H
2

You may considering using the LinkedIn PHP SDK (provided by the community) instead: https://github.com/Happyr/LinkedIn-API-client

Hemimorphic answered 19/4, 2015 at 17:46 Comment(1)
Are there any limits for the LinkedIn API I need to be aware of? (Timeouts, max. sizes for image URLs...)Winna
B
1

We faced similar issue with Linkedin API recently. Finally figured out the fix by changing the url.

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

Instead of specifying 'oauth2_access_token' in the query string, add it in the header - specify :

"Authorization", "Bearer " + accessToken.

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

Bubonocele answered 24/4, 2015 at 10:30 Comment(0)
P
1

You have to Use your Authentication Token in Request Headers.

This is the working code. Try it.

$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);
Poetess answered 26/4, 2015 at 0:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.