php - Why Share on LinkedIn API return 400 Bad Request? -
i'm trying post linkedin share update via share on linkedin api using php library linkedin-client api. i've authorized user , got access token returned it. use token along share api call. here code:
$linkedinoauth = new happyr\linkedin\linkedin(linkedin_app_id, linkedin_app_secret); if ($accesstoken) { // assume retrieved session $linkedinoauth->setaccesstoken($accesstoken); $postparams = array( "content" => array( 'description' => "i'm exciting share post using api." ), "visibility" => array( "code" => "connnections-only" ) ); $result = $linkedinoauth->api( "v1/people/~/shares", array("format" => "json"), "post", $postparams ); }
however got 400 bad request error returned api call.
fatal error: uncaught guzzleexception: 400: client error response [url] https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=xxxxxxx [status code] 400 [reason phrase] bad request thrown in ...\vendor\happyr\linkedin-api-client\src\happyr\linkedin\http\guzzlerequest.php on line 26
what problem?
[update on 2015-04-30 3:00 pm utc]
linkedin-client api uses guzzle internally http requests. tried use guzzlehttp
directly without using happyr\linkedin\linkedin->api()
, same error , no success.
if ($accesstoken) { $url = 'https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=' . $accesstoken; $client = new guzzlehttp\client(); $response = $client->post($url, array( 'headers' => array( 'content-type' => 'application/json', 'x-li-format' => 'json' ), 'json' => array( 'comment' => 'check out developer.linkedin.com!', 'content' => array( 'description' => 'i\'m exciting share post using api.' ), 'visibility' => array( 'code' => 'connections-only' ) ) )); }
fatal error: uncaught exception 'guzzlehttp\exception\clientexception' message 'client error response [url] https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=xxxxx [status code] 400 [reason phrase] bad request' in \vendor\guzzlehttp\guzzle\src\exception\requestexception.php:89 stack trace: #0 \vendor\guzzlehttp\guzzle\src\subscriber\httperror.php(33): guzzlehttp\exception\requestexception::create(object(guzzlehttp\message\request), object(guzzlehttp\message\response)) #1 \vendor\guzzlehttp\guzzle\src\event\emitter.php(109): guzzlehttp\subscriber\httperror->oncomplete(object(guzzlehttp\event\completeevent), 'complete') #2 \vendor\guzzlehttp\guzzle\src\requestfsm.php(91): guzzlehttp\event\emitter->emit('complete', object(guz in \vendor\guzzlehttp\guzzle\src\exception\requestexception.php on line 89
[update on 2015-05-05 9:40 utc]
i copied , used both xml , json examples of share on linkedin api page. time, error changed internal server error.
if ($accesstoken) { $format = 'xml'; $url = 'https://api.linkedin.com/v1/people/~/shares?format='.$format.'&oauth2_access_token=' . $connect->accesstoken; $postparams = array( "xml" => " <share> <comment>check out developer.linkedin.com!</comment> <content> <title>linkedin developer resources</title> <description>leverage linkedin's apis maximize engagement</description> <submitted-url>https://developer.linkedin.com</submitted-url> <submitted-image-url>https://example.com/logo.png</submitted-image-url> </content> <visibility> <code>anyone</code> </visibility> </share>", "json" => array( "comment" => "check out developer.linkedin.com!", "content" => array( "title" => "linkedin developers resources", "description" => "leverage linkedin's apis maximize engagement", "submitted-url" => "https://developer.linkedin.com", "submitted-image-url" => "https://example.com/logo.png" ), "visibility" => array( "code" => "anyone" ) ) ); $client = new guzzlehttp\client(); if ($format === 'xml') { $response = $client->post($url, array( 'body' => $postparams['xml'] )); } else { $response = $client->post($url, array( 'headers' => array( 'content-type' => 'application/json', 'x-li-format' => 'json' ), 'json' => $postparams['json'] )); } }
fatal error: uncaught exception 'guzzlehttp\exception\serverexception' message 'server error response [url] https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=xxxx [status code] 500 [reason phrase] internal server error'
according the github issue of happyr\linkedin-api-client, updates have been made in 0.5.0 , solved problem. however, linkedin has unclear documentation share api. following information have noted:
- the
comment
field sharing update content. namecomment
leads confusing. - using field
comment
, url optional in sharing update content. - the
content
field means snapshot content of url sharing. describe more clearly, reflects open graph meta tags;content.title
overrides<meta property="og:title" content="..." />
content.description
overrides<meta property="description" content="..." />
content.title
overrides<meta property="og:title" content="..." />
content.submitted-url
overrides<meta property="og:url" content="..." />
content.submitted-image-url
overrides<meta property="og:image" content="..." />
- when
content
field used, fieldsubmitted-url
required. rest optional. if missing, 400 bad request return. - the urls included in example codes of linkedin share api cause 500 internal server error. should not used testing purpose.
the following correct usage of api using happyr\linkedin-api-client.
(1) happyr\linkedin - using field comment
$linkedinoauth = new happyr\linkedin\linkedin(linkedin_app_id, linkedin_app_secret); // retrieve $accesstoken db or session $linkedinoauth->setaccesstoken($accesstoken); $postparams = array( 'json' => array( "comment" => "phplucidframe - simple, lightweight , flexible web application development framework http://phplucidframe.sithukyaw.com", "visibility" => array( "code" => "anyone" ) ) ); $result = $linkedinoauth->post('v1/people/~/shares', $postparams);
(2) happyr\linkedin - using field content
$linkedinoauth = new happyr\linkedin\linkedin(linkedin_app_id, linkedin_app_secret); // retrieve $accesstoken db or session $linkedinoauth->setaccesstoken($accesstoken); $postparams = array( 'json' => array( "content" => array( "title" => "phplucidframe", "description" => "the simple, lightweight , flexible web application development framework", "submitted-url" => "http://phplucidframe.sithukyaw.com" ), "visibility" => array( "code" => "anyone" ) ) ); $result = $linkedinoauth->post('v1/people/~/shares', $postparams);
the following correct usage of api using guzzlehttp.
(3) guzzlehttp - using field comment
// retrieve $accesstoken db or session $url = 'https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=' . $accesstoken; $client = new guzzlehttp\client(); $response = $client->post($url, array( "json" => array( "comment" => "phplucidframe - simple, lightweight , flexible web application development framework http://phplucidframe.sithukyaw.com", "visibility" => array( "code" => "anyone" ) ) ));
(4) guzzlehttp - using field content
// retrieve $accesstoken db or session $url = 'https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=' . $accesstoken; $client = new guzzlehttp\client(); $response = $client->post($url, array( "json" => array( "content" => array( "title" => "phplucidframe", "description" => "the simple, lightweight , flexible web application development framework", "submitted-url" => "http://phplucidframe.sithukyaw.com" ), "visibility" => array( "code" => "anyone" ) ) ));
the fields comment
, content
can used together.
Comments
Post a Comment