如何通过API从LinkedIn获得公司帖子?

时间:2019-09-13 16:08:27

标签: php http-status-code-404 linkedin linkedin-api

我想用PHP从LinkedIn获得公司职位。我应该使用什么请求来发帖?

我尝试使用PHP库"zoonman/linkedin-api-php-client"。我创建了具有对公司的经过验证的访问权的LinkedIn应用程序。我编写了获取LinkenIn访问令牌的代码。我尝试使用GET 'companies' request,但收到错误404或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);
    }

我收到客户端错误(“ GuzzleHttp \ Exception \ ClientException”),具体取决于请求的地址:

  • GET https://api.linkedin.com/companies/10239645/updates?format=json产生了404 Not Found响应”
  • GET https://api.linkedin.com/v1/companies/10239645/updates?format=json产生了410 Gone响应:{“ errorCode”:0,“ message”:“此资源在v1 API下不再可用”,“ requestId”:“ CH17ME6WK6”, “ s(截断...)”;
  • GET https://api.linkedin.com/v2/companies/10239645/updates?format=json产生了404 Not Found响应:{“ serviceErrorCode”:0,“ message”:“资源公司不存在”,“ status”:404}“。

我想从LinkedIn获得公司的帖子。

请求“ companies”是否已被禁用?我应该使用什么请求(和请求地址)?如何立即从LinkedIn获取公司帖子?我的代码有什么问题?你能帮我吗?

1 个答案:

答案 0 :(得分:0)

您应该使用Find Share By Owner端点。这是第2版端点,因此要做的第一件事是按照SDK文档中的说明Change default API root

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

然后尝试:

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

NB:检查您是否在OAuth流程中要求正确的范围(请参阅页面开头的permission部分),并且您的应用具有正确的权限(请参阅开发者应用中的产品部分)控制台)