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