Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get company posts from LinkedIn via API?

I would like to get the company posts from LinkedIn in PHP. What request should I use to ask for the post?

I try to use PHP library "zoonman/linkedin-api-php-client". I have created LinkedIn application with a verified access to the company. I wrote the code that get LinkenIn access token. I try to use GET 'companies' request but I receive errors 404 or 410.

    <?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    // add Composer autoloader
    include_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor/autoload.php';

    // import client class
    use LinkedIn\Client;
    use LinkedIn\Scope;
    use LinkedIn\AccessToken;

    // instantiate the Linkedin client
    $client = new Client(
        'correct client id',
        'correct client secret'
    );
    $scopes = [
        Scope::READ_BASIC_PROFILE
      ];
    $client->setRedirectUrl("correct return address, which is the same as URL of current script");
    $loginUrl = $client->getLoginUrl($scopes);

    if (isset($_GET) && isset($_GET['code']))
    {
        $accessToken = $client->getAccessToken($_GET['code']);
        $client->setAccessToken($accessToken);
        $client->setApiRoot('https://api.linkedin.com/v1/'); //I've tried to use 'https://api.linkedin.com/v2/' too
        $client->setApiHeaders([
            'Content-Type' => 'application/json',
            'x-li-format' => 'json',
            'X-Restli-Protocol-Version' => '2.0.0', // use protocol v2
        ]);
        $posts = $client->get('/v1/companies/10239645/updates?format=json'); 
        //I've tried to use '/v2/companies/10239645/updates?format=json' 
        //and '/companies/10239645/updates?format=json'
        var_dump($posts);
        die();
    }
    else
    {
        header('Location: ' . $loginUrl);
    }

I receive client errors ('GuzzleHttp\Exception\ClientException') depending on the address of the request:

  • "GET https://api.linkedin.com/companies/10239645/updates?format=json resulted in a 404 Not Found response"
  • "GET https://api.linkedin.com/v1/companies/10239645/updates?format=json resulted in a 410 Gone response: { "errorCode": 0, "message": "This resource is no longer available under v1 APIs", "requestId": "CH17ME6WK6", "s (truncated...)";
  • "GET https://api.linkedin.com/v2/companies/10239645/updates?format=json resulted in a 404 Not Found response: {"serviceErrorCode":0,"message":"Resource companies does not exist","status":404}".

I would like to get the company posts from LinkedIn.

Is the request 'companies' is disabled now? What request (and request address) should I use? How to get the company posts from LinkedIn now? What is wrong in my code? Could you help me?

like image 620
Kamil Szmit Avatar asked Sep 13 '19 16:09

Kamil Szmit


1 Answers

You should use the Find Share By Owner endpoint. This is a v2 endpoint so the first thing to do is to Change default API root as described in the doc of the SDK:

  $client->setApiRoot('https://api.linkedin.com/v2/');

Then try with:

$posts = $client->get('shares?q=owners&owners=urn:li:organization:10239645'); 

NB: Check you are asking for the right Scopes during the OAuth flow (See the permission section at the beginning of the page) and that your App have the right permission (See the product section in the developer app console)

like image 191
Matteo Avatar answered Sep 28 '22 16:09

Matteo