Laravel Socialite extension fields
Basically, I want to get a link to the user's profile from Linkedin. I am using Laravel Socialite with Socialist Providers to get information from Linkedin.
When the user is redirected to my site with success, I debug the information:
User {#285 ▼
+token: "XXX"
+id: "XXX"
+nickname: null
+name: "XXX"
+email: "XXX"
+avatar: "XXX"
+"user": array:4 [▶]
}
So, I wanted to expand this information with "public-profile-url", this is the profile profile field from Linkedin.
I tried to do something like this in "myproject / vendor / socialiteproviders / linkedin / src / Provider.php":
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User())->setRaw($user)->map([
'id' => $user['id'], 'nickname' => null,
'name' => $user['formattedName'], 'email' => $user['emailAddress'],
'avatar' => array_get($user, 'pictureUrl'),
'link' => array_get($user, 'publicProfileUrl'),
]);
}
But then the link will be "null".
Does anyone know how to fix this problem?
source to share
I fixed the problem.
In myproject / vendor / socialiteproviders / linkedin / src / Provider.php I added the 'public-profile-url' field to the url:
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get(
'https://api.linkedin.com/v1/people/~:(id,formatted-name,picture-url,email-address,public-profile-url)', [
'headers' => [
'Accept-Language' => 'en-US',
'x-li-format' => 'json',
'Authorization' => 'Bearer '.$token,
],
]);
return json_decode($response->getBody(), true);
}
With this, you can access the "publicProfileUrl" field in the users array, for example:
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User())->setRaw($user)->map([
'id' => $user['id'], 'nickname' => null,
'name' => $user['formattedName'], 'email' => $user['emailAddress'],
'avatar' => array_get($user, 'pictureUrl'),
'profileUrl' => array_get($user, 'publicProfileUrl'),
]);
}
Hope someone finds this helpful.
Notice
This is a supplier directory! This code can be thrown when you perform (composer) update on your project.
source to share