2015-04-16 16 views
6

Sto utilizzando lo LinkedIn REST API per pubblicare aggiornamenti sulla timeline di un utente. Da alcuni giorni ricevo una risposta Internal server error da LinkedIn ma il codice funzionava prima.API REST LinkedIn - Errore interno server

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

Come risolvere tale errore?

risposta

4

Il tuo codice non è valido PHP (forse a causa di alcune modifiche apportate prima di postare?); modificandolo a:

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

opere se solo $oauthToken è impostato su un gettone valido. Supponendo che il tuo codice reale sia corretto, l'unica possibilità rimasta è che il tuo token OAuth sia scaduto e devi prima ottenerne uno nuovo. Aggiungendo CURLOPT_VERBOSE => TRUE alle opzioni cURL, si scoprirà di più sull'errore restituito da LinkedIn.

+0

appena provato il codice con un gettone valido e ottenuto lo stesso "errore interno del servizio": 'risposta: { "errorCode": 0, "messaggio": "errore interno del servizio", "IDRichiesta": "J6PAQ9 "," status ": 500," timestamp ": 1429742}' Qualche idea? – Tom

2

Si può considerare utilizzando il LinkedIn PHP SDK (fornito dalla comunità), invece: https://github.com/Happyr/LinkedIn-API-client

+0

Ci sono dei limiti per l'API di LinkedIn di cui ho bisogno di essere a conoscenza? (Timeout, dimensioni massime per gli URL delle immagini ...) – Tom

1

abbiamo affrontato problema simile con Linkedin API di recente. Finalmente capito la correzione cambiando l'url.

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

invece di specificare 'oauth2_access_token' nella stringa di query, aggiungerlo nell'intestazione - specificare:

"Autorizzazione", "portatore" + access token.

E infine nel parametro corpo della richiesta, aggiungere i dati JSON/XML per pubblicare

1

Devi usare il tuo token di autenticazione in intestazioni di richiesta.

Questo è il codice funzionante. Provalo.

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