1
|
|
|
<?php namespace Arcanedev\Socialite\OAuth\Two; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Socialite\Base\OAuthTwoProvider; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class LinkedInProvider |
7
|
|
|
* |
8
|
|
|
* @package Arcanedev\Socialite\OAuth\Two |
9
|
|
|
* @author ARCANEDEV <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class LinkedInProvider extends OAuthTwoProvider |
12
|
|
|
{ |
13
|
|
|
/* ------------------------------------------------------------------------------------------------ |
14
|
|
|
| Properties |
15
|
|
|
| ------------------------------------------------------------------------------------------------ |
16
|
|
|
*/ |
17
|
|
|
/** |
18
|
|
|
* The scopes being requested. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $scopes = ['r_basicprofile', 'r_emailaddress']; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The fields that are included in the profile. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $fields = [ |
30
|
|
|
'id', 'first-name', 'last-name', 'formatted-name', |
31
|
|
|
'email-address', 'headline', 'location', 'industry', |
32
|
|
|
'public-profile-url', 'picture-url', 'picture-urls::(original)', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/* ------------------------------------------------------------------------------------------------ |
36
|
|
|
| Getters & Setters |
37
|
|
|
| ------------------------------------------------------------------------------------------------ |
38
|
|
|
*/ |
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
protected function getAuthUrl($state) |
43
|
|
|
{ |
44
|
|
|
return $this->buildAuthUrlFromBase('https://www.linkedin.com/uas/oauth2/authorization', $state); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
protected function getTokenUrl() |
51
|
|
|
{ |
52
|
|
|
return 'https://www.linkedin.com/uas/oauth2/accessToken'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the POST fields for the token request. |
57
|
|
|
* |
58
|
|
|
* @param string $code |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
protected function getTokenFields($code) |
63
|
|
|
{ |
64
|
|
|
return parent::getTokenFields($code) + ['grant_type' => 'authorization_code']; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
protected function getUserByToken($token) |
71
|
|
|
{ |
72
|
|
|
$fields = implode(',', $this->fields); |
73
|
|
|
$url = 'https://api.linkedin.com/v1/people/~:('.$fields.')'; |
74
|
|
|
$response = $this->getHttpClient()->get($url, [ |
75
|
|
|
'headers' => [ |
76
|
|
|
'x-li-format' => 'json', |
77
|
|
|
'Authorization' => 'Bearer '.$token, |
78
|
|
|
], |
79
|
|
|
]); |
80
|
|
|
|
81
|
|
|
return json_decode($response->getBody(), true); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
protected function mapUserToObject(array $user) |
88
|
|
|
{ |
89
|
|
|
return (new User)->setRaw($user)->map([ |
90
|
|
|
'id' => $user['id'], |
91
|
|
|
'nickname' => null, |
92
|
|
|
'name' => array_get($user, 'formattedName'), |
93
|
|
|
'email' => array_get($user, 'emailAddress'), |
94
|
|
|
'avatar' => array_get($user, 'pictureUrl'), |
95
|
|
|
'avatar_original' => array_get($user, 'pictureUrls.values.0'), |
96
|
|
|
]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set the user fields to request from LinkedIn. |
101
|
|
|
* |
102
|
|
|
* @param array $fields |
103
|
|
|
* |
104
|
|
|
* @return self |
105
|
|
|
*/ |
106
|
|
|
public function fields(array $fields) |
107
|
|
|
{ |
108
|
|
|
$this->fields = $fields; |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|