【问题标题】:LinkedIn returns 401: unknown authentication scheme when trying to shareLinkedIn 尝试共享时返回 401:未知身份验证方案
【发布时间】:2016-02-25 04:49:27
【问题描述】:

我正在尝试在我们的项目中实现到 LinkedIn 的共享。它对我来说就像一种魅力,然而,对我所有的同事来说,它都没有。它返回401: Unknown authentication scheme 仅用于共享请求(配置文件请求工作正常)。

我们使用的 LinkedIn 应用程序归我所有。出于测试目的,另一位开发者试图在他的帐户中创建一个应用程序,但问题仍然存在:我是唯一可以分享的人。

我们使用linkedinapi/linkedin 作为后端。代码如下:

protected function openConnection()
{
    $credentials = \Config::get('services.linkedin');

    try
    {
        $this->connection = new \LinkedIn\LinkedIn([
            'api_key' => $credentials['client_id'],
            'api_secret' => $credentials['client_secret'],
            'callback_url' => $credentials['redirect'],
        ]);

        $this->connection->setAccessToken(\Auth::user()->linkedin->token['access_token']);
    }
    catch (\Exception $e)
    {
        \Log::error('Failed LinkedIn auth, exception is attached', [$e]);

        throw new CouldNotPostException('Please, re-link your LinkedIn account, failed to send your post to LinkedIn.');
    }
}

protected function send()
{
    $object = [
        'content' => [
            'title' => $this->post->title,
            'description' => \Str::limit($this->post->description, 253),
            'submitted-url' => $this->post->shortlink,
            'submitted_image_url' => \URL::to($this->post->image),
        ],
        'comment' => $this->content,
        'visibility' => [
            'code' => 'connections-only'
        ]
    ];

    try
    {
        $result = $this->connection->post('/people/~/shares?format=json', $object);

        $this->posted($result['updateKey']);
    }
    catch (\Exception $e)
    {
        \Log::error('Failed LinkedIn posting, exception is attached', [$e]);

        throw new CouldNotPostException('Failed to send your post to LinkedIn, our developers have been notified of this error.');
    }
}

UPD:原来只能在我的机器上发帖,所以他们机器上的配置有问题。正在寻找它。

【问题讨论】:

    标签: php rest linkedin linkedin-api


    【解决方案1】:

    调查

    我让我的同事在我的机器上登录 LinkedIn,查看他的帐户或计算机上的发帖是否失败。它发布了,这表明我的配置有问题,这就是它起作用的原因。

    我的下一步是重新安装所有供应商软件包,包括上面提到的linkedinapi/linkedin。在那之后,我也不再为我工作了。在检查了图书馆签署请求的方式后,我发现它与 LinkedIn 要求的方式不符。该库在 URL 中包含一个 oauth2_access_token 参数,但 LinkedIn 需要一个 Authorization HTTP 标头,如其文档中所述:

    获得访问令牌后,您就可以开始代表用户发出经过身份验证的 API 请求。这是通过在对 LinkedIn 的 API 的 HTTP 调用中包含“授权”标头来实现的。

    解决方案

    所以现在我改变了我提出请求的方式。而不是

    $result = $this->connection->post('/people/~/shares?format=json', $object);
    

    我使用了一个不同的功能,允许我手动包含标题:

    $result = $this->connection->fetch(
        '/people/~/shares?format=json',
        $object,
        \LinkedIn\LinkedIn::HTTP_METHOD_POST,
        [ 'Authorization: Bearer ' . $this->connection->getAccessToken() ]
    );
    

    另外,我创建了一个到存储库的拉取请求。

    【讨论】:

      猜你喜欢
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 2019-09-13
      • 1970-01-01
      • 2017-09-05
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多