LinkedIn returns 401:尝试共享时身份验证方案未知
LinkedIn returns 401: unknown authentication scheme when trying to share
我正在尝试在我们的项目中实现分享到LinkedIn。它对我来说就像一种魅力,但对我所有的同事来说却并非如此。它 returns 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: 原来它只能在我的机器上 post ,所以他们机器上的配置有问题。正在寻找。
调查
我让我的同事在我的机器上登录 LinkedIn,看看他的帐户或他的电脑是否发帖失败。它发布了,这表明我的配置有问题,这就是它起作用的原因。
我的下一步是 re-install 所有供应商包,包括上面提到的 linkedinapi/linkedin。在那之后,我也停止了发帖。在检查了图书馆签署请求的方式后,我发现它不符合 LinkedIn 要求的方式。该库在 URL 中包含一个 oauth2_access_token
参数,但 LinkedIn 期望一个 Authorization
HTTP header,如其文档中所述:
Once you've obtained an Access Token, you can start making authenticated API requests on behalf of the user. This is accomplished by including an "Authorization" header in your HTTP call to LinkedIn's API.
解决方案
所以现在我改变了我提出请求的方式。而不是
$result = $this->connection->post('/people/~/shares?format=json', $object);
我使用了一个不同的函数,允许我手动包含 header:
$result = $this->connection->fetch(
'/people/~/shares?format=json',
$object,
\LinkedIn\LinkedIn::HTTP_METHOD_POST,
[ 'Authorization: Bearer ' . $this->connection->getAccessToken() ]
);
另外,我向存储库创建了拉取请求。
我正在尝试在我们的项目中实现分享到LinkedIn。它对我来说就像一种魅力,但对我所有的同事来说却并非如此。它 returns 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: 原来它只能在我的机器上 post ,所以他们机器上的配置有问题。正在寻找。
调查
我让我的同事在我的机器上登录 LinkedIn,看看他的帐户或他的电脑是否发帖失败。它发布了,这表明我的配置有问题,这就是它起作用的原因。
我的下一步是 re-install 所有供应商包,包括上面提到的 linkedinapi/linkedin。在那之后,我也停止了发帖。在检查了图书馆签署请求的方式后,我发现它不符合 LinkedIn 要求的方式。该库在 URL 中包含一个 oauth2_access_token
参数,但 LinkedIn 期望一个 Authorization
HTTP header,如其文档中所述:
Once you've obtained an Access Token, you can start making authenticated API requests on behalf of the user. This is accomplished by including an "Authorization" header in your HTTP call to LinkedIn's API.
解决方案
所以现在我改变了我提出请求的方式。而不是
$result = $this->connection->post('/people/~/shares?format=json', $object);
我使用了一个不同的函数,允许我手动包含 header:
$result = $this->connection->fetch(
'/people/~/shares?format=json',
$object,
\LinkedIn\LinkedIn::HTTP_METHOD_POST,
[ 'Authorization: Bearer ' . $this->connection->getAccessToken() ]
);
另外,我向存储库创建了拉取请求。