简体   繁体   中英

Why Share on LinkedIn API return 400 Bad Request?

I'm trying to post a LinkedIn share update via its Share on LinkedIn API using a PHP library LinkedIn-Client API . I've successfully authorized the user and I got the access token returned from it. I use the token along with this share API call. Here is my code:

$linkedInOAuth = new Happyr\LinkedIn\LinkedIn(LINKEDIN_APP_ID, LINKEDIN_APP_SECRET);

if ($accessToken) { // assume that it is retrieved from session
    $linkedInOAuth->setAccessToken($accessToken);

    $postParams = array(
        "content" => array(
            'description' => "I'm so exciting to share a post using API."
        ),
        "visibility" => array(
            "code" => "connnections-only"
        )
    );

    $result = $linkedInOAuth->api(
        "v1/people/~/shares",
        array("format" => "json"),
        "POST",
        $postParams
    );
}

However I got 400 bad request error returned from this 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 could be the problem?

[UPDATE on 2015-04-30 3:00 PM UTC]

LinkedIn-Client API uses Guzzle internally for HTTP requests. I tried to use GuzzleHttp directly without using Happyr\\LinkedIn\\LinkedIn->api() , but same error and 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 so exciting to share a post using API.'
            ),
            'visibility' => array(
                'code' => 'connections-only'
            )
        )
    ));
}

Fatal error: Uncaught exception 'GuzzleHttp\\Exception\\ClientException' with 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 AM UTC]

I copied and used both xml and json examples of Share on LinkedIn API page . This time, the error changed to 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 to 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 to 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' with 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 to the Github issue of Happyr\\LinkedIn-API-client , some updates have been made in 0.5.0 and it solved my problem. However, LinkedIn has unclear documentation about its Share API. The following information have to be noted:

  1. The comment field is the sharing update content. The name comment leads confusing.
  2. Using the field comment , URL is optional in the sharing update content.
  3. The content field means snapshot about the content of the URL you are sharing. To describe more clearly, it 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="..." />
  4. When the content field is used, the field submitted-url is required. The rest are optional. If it is missing, 400 Bad Request would return.
  5. The URLs included in the example codes of LinkedIn Share API would cause 500 Internal Server Error . They should not be used for testing purpose.

The following is the correct usage of API using Happyr\\LinkedIn-API-client .

(1) Happyr\\LinkedIn - Using the field comment

$linkedInOAuth = new Happyr\LinkedIn\LinkedIn(LINKEDIN_APP_ID, LINKEDIN_APP_SECRET);
// retrieve $accessToken from db or session
$linkedInOAuth->setAccessToken($accessToken);

$postParams = array(
    'json' => array(
        "comment" => "PHPLucidFrame - The simple, lightweight and flexible web application development framework http://phplucidframe.sithukyaw.com",
        "visibility" => array(
            "code" => "anyone"
        )
    )
);

$result = $linkedInOAuth->post('v1/people/~/shares', $postParams);

(2) Happyr\\LinkedIn - Using the field content

$linkedInOAuth = new Happyr\LinkedIn\LinkedIn(LINKEDIN_APP_ID, LINKEDIN_APP_SECRET);
// retrieve $accessToken from db or session
$linkedInOAuth->setAccessToken($accessToken);

$postParams = array(
    'json' => array(
        "content" => array(
            "title" => "PHPLucidFrame",
            "description" => "The simple, lightweight and flexible web application development framework",
            "submitted-url" => "http://phplucidframe.sithukyaw.com"
        ),
        "visibility" => array(
            "code" => "anyone"
        )
    )
);

$result = $linkedInOAuth->post('v1/people/~/shares', $postParams);

The following is the correct usage of API using GuzzleHttp .

(3) GuzzleHttp - Using the field comment

// retrieve $accessToken from 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 - The simple, lightweight and flexible web application development framework http://phplucidframe.sithukyaw.com",
        "visibility" => array(
            "code" => "anyone"
        )
    )
));

(4) GuzzleHttp - Using the field content

// retrieve $accessToken from 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 and flexible web application development framework",
            "submitted-url" => "http://phplucidframe.sithukyaw.com"
        ),
        "visibility" => array(
            "code" => "anyone"
        )
    )
));

The fields comment and content can also be used together.

Just a quick guess here but you might need to add json_encode to your $postParams before you send it to the API.

$result = $linkedInOAuth->api(
    "v1/people/~/shares",
    array("format" => "json"),
    "POST",
    json_encode($postParams),
);

ALTERNATIVELY

If that does not work I also notice the Linkedin Docs say the following. You could try adding these two headers (if you havent already).

By default, all API calls expect input in XML format, however if it is more covienent for your application to submit data in JSON format, you can inform the APIs that they will be receiving a JSON-formatted payload by including the following two HTTP header values in the call:

Content-Type: application/json

x-li-format: json

Your JSON post body needs to be corrected. Check:

https://developer.linkedin.com/docs/share-on-linkedin

You need to follow either one of these formats.

  1. Share via a comment containing a URL

     { "comment": "Check out developer.linkedin.com! http://linkd.in/1FC2PyG", "visibility": { "code": "anyone" } } 
  2. Share with specific values

     { "comment": "Check out developer.linkedin.com!", "content": { "title": "LinkedIn Developers Resources", "description": "Leverage LinkedIn's APIs to maximize engagement", "submitted-url": "https://developer.linkedin.com", "submitted-image-url": "https://example.com/logo.png" }, "visibility": { "code": "anyone" } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM