How to get company posts from LinkedIn via API?
Asked Answered
S

1

9

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?

Scent answered 13/9, 2019 at 16:8 Comment(1)
Hello Kamil, any news on the subject ? I'm used with several wrappers, but am struggling with Linkedin. Thank you.Alamo
C
4

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)

Cowry answered 13/9, 2019 at 16:56 Comment(7)
Sending request to 'shares?q=owners&owners=urn:li:organization:10239645' causes error "GET https://api.linkedin.com/v2/shares?q=owners&owners=urn:li:organization:10239645 resulted in a 400 Bad Request response: {"serviceErrorCode":0,"message":"Invalid query parameters passed to request","status":400}".Scent
@KamilSzmit very strange, you can debug the endpoint with your browser with the url: https://api.linkedin.com/v2/shares?q=owners&owners=urn:li:organization:10239645&oauth2_access_token=<ACCESS_TOKEN> using a valid access tokenCowry
The address api.linkedin.com/v2/…> returns {"serviceErrorCode":100,"message":"Not enough permissions to access: GET-owners /shares","status":403} Does this query require a product 'Marketing Developer Platform' added to the LinkedIn application? Does this query really return the LinkedIn posts of the company?Scent
I am having a same issue.Hardwick
I am having the same issueGertrudis
@KamilSzmit yes it requires additional permission as page managment is part of the marketing development platform or whatever LinkedIn calls itDegust
@Cowry I have the necessary permissions and still get Resource organization does not exist', 'status': 404 also with your suggestion one would only get the posts that a specific individual posted via company page and not all company posts?Degust

© 2022 - 2024 — McMap. All rights reserved.