Kamil Szmit
Kamil Szmit

Reputation: 93

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:

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?

Upvotes: 9

Views: 11367

Answers (1)

Matteo
Matteo

Reputation: 39390

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)

Upvotes: 4

Related Questions