I am trying to implement sharing to LinkedIn in our project. It works for me like a charm, however, for all of my colleagues, it does not. It returns 401: Unknown authentication scheme
just for sharing request (profile requests work fine).
The LinkedIn app that we use is owned by me. For testing purposes another developer tried to create an app in his account, but the problem stayed: I was the only one who could share.
We are using linkedinapi/linkedin for back-end. Here's the code:
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: turns out it can post only on my machine, so there is something wrong with the configuration on their machines. Looking for it.
Investigation
I asked my colleague to login to LinkedIn on my machine, to see if posting fails for his account or for his computer. It posted, which showed that something is up with my configuration, that is why it works.
My next step was to re-install all of the vendor packages, including the mentioned above linkedinapi/linkedin. After that posting stopped working for me as well. After inspecting the way that library signs requests, I found that it does not match the way LinkedIn requires it. The library was including a oauth2_access_token
parameter in the URL, but LinkedIn expects an Authorization
HTTP header as it is stated in their documentation:
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.
Solution
So for now I changed the way I make the request. Instead of
$result = $this->connection->post('/people/~/shares?format=json', $object);
I use a different function that allows me to include the header manually:
$result = $this->connection->fetch(
'/people/~/shares?format=json',
$object,
\LinkedIn\LinkedIn::HTTP_METHOD_POST,
[ 'Authorization: Bearer ' . $this->connection->getAccessToken() ]
);
Also, I created a pull request to the repository.