Skip to content

Commit

Permalink
[OAuth2] Provider/LinkedIn - migrate to v2 API #97
Browse files Browse the repository at this point in the history
  • Loading branch information
ovr committed Oct 14, 2019
1 parent 4872935 commit 957ed24
Showing 1 changed file with 42 additions and 7 deletions.
49 changes: 42 additions & 7 deletions src/OAuth2/Provider/LinkedIn.php
Expand Up @@ -21,7 +21,7 @@ class LinkedIn extends \SocialConnect\OAuth2\AbstractProvider
*/
public function getBaseUri()
{
return 'https://api.linkedin.com/v1/';
return 'https://api.linkedin.com/v2/';
}

/**
Expand Down Expand Up @@ -53,7 +53,7 @@ public function getName()
*/
public function prepareRequest(string $method, string $uri, array &$headers, array &$query, AccessTokenInterface $accessToken = null): void
{
$query['format'] = 'json';
$headers['Content-Type'] = 'application/json';

if ($accessToken) {
$headers['Authorization'] = "Bearer {$accessToken->getToken()}";
Expand All @@ -65,19 +65,54 @@ public function prepareRequest(string $method, string $uri, array &$headers, arr
*/
public function getIdentity(AccessTokenInterface $accessToken)
{
$query = [];

$fields = $this->getArrayOption(
'identity.fields',
[
'id',
'firstName',
'lastName',
'emailAddress',
'profilePicture(displayImage~:playableStreams)',
'location'
]
);
if ($fields) {
$query['projection'] = '(' . implode(',', $fields) . ')';
}

$response = $this->request(
'GET',
'people/~:(id,first-name,last-name,email-address,picture-url,location:(name))',
[],
'me',
$query,
$accessToken
);

$hydrator = new ArrayHydrator([
'id' => 'id',
'emailAddress' => 'email',
'firstName' => 'firstname',
'lastName' => 'lastname',
'pictureUrl' => 'pictureURL',
'firstName' => static function ($value, User $user) {
if ($value['localized']) {
$user->firstname = array_pop($value['localized']);
}
},
'lastName' => static function ($value, User $user) {
if ($value['localized']) {
$user->lastname = array_pop($value['localized']);
}
},
'profilePicture' => static function ($value, User $user) {
if (isset($value['displayImage~']) && isset($value['displayImage~']['elements'])) {
$biggestElement = array_shift($value['displayImage~']['elements']);
if (isset($biggestElement['identifiers'])) {
$biggestElementIdentifier = array_pop($biggestElement['identifiers']);
if (isset($biggestElementIdentifier['identifier'])) {
$user->pictureURL = $biggestElementIdentifier['identifier'];
}
}
}
},
]);

return $hydrator->hydrate(new User(), $response);
Expand Down

0 comments on commit 957ed24

Please sign in to comment.