1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\OAuth2\Client\Provider; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
7
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
8
|
|
|
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
class LinkedIn extends AbstractProvider |
12
|
|
|
{ |
13
|
|
|
use BearerAuthorizationTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Default scopes |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
public $defaultScopes = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Requested fields in scope, seeded with default values |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
* @see https://developer.linkedin.com/docs/fields/basic-profile |
27
|
|
|
*/ |
28
|
|
|
protected $fields = [ |
29
|
|
|
'id', 'email-address', 'first-name', 'last-name', 'headline', |
30
|
|
|
'location', 'industry', 'picture-url', 'public-profile-url', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructs an OAuth 2.0 service provider. |
35
|
|
|
* |
36
|
|
|
* @param array $options An array of options to set on this provider. |
37
|
|
|
* Options include `clientId`, `clientSecret`, `redirectUri`, and `state`. |
38
|
|
|
* Individual providers may introduce more options, as needed. |
39
|
|
|
* @param array $collaborators An array of collaborators that may be used to |
40
|
|
|
* override this provider's default behavior. Collaborators include |
41
|
|
|
* `grantFactory`, `requestFactory`, and `httpClient`. |
42
|
|
|
* Individual providers may introduce more collaborators, as needed. |
43
|
|
|
*/ |
44
|
30 |
|
public function __construct(array $options = [], array $collaborators = []) |
45
|
|
|
{ |
46
|
30 |
|
if (isset($options['fields']) && !is_array($options['fields'])) { |
47
|
3 |
|
throw new InvalidArgumentException('The fields option must be an array'); |
48
|
|
|
} |
49
|
|
|
|
50
|
30 |
|
parent::__construct($options, $collaborators); |
51
|
30 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the string used to separate scopes. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
9 |
|
protected function getScopeSeparator() |
59
|
|
|
{ |
60
|
9 |
|
return ' '; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get authorization url to begin OAuth flow |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
9 |
|
public function getBaseAuthorizationUrl() |
69
|
|
|
{ |
70
|
9 |
|
return 'https://www.linkedin.com/oauth/v2/authorization'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get access token url to retrieve token |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
15 |
|
public function getBaseAccessTokenUrl(array $params) |
79
|
|
|
{ |
80
|
15 |
|
return 'https://www.linkedin.com/oauth/v2/accessToken'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get provider url to fetch user details |
85
|
|
|
* |
86
|
|
|
* @param AccessToken $token |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
6 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
91
|
|
|
{ |
92
|
6 |
|
$fields = implode(',', $this->fields); |
93
|
|
|
|
94
|
6 |
|
return 'https://api.linkedin.com/v1/people/~:(' . $fields . ')?format=json'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the default scopes used by this provider. |
99
|
|
|
* |
100
|
|
|
* This should not be a complete list of all scopes, but the minimum |
101
|
|
|
* required for the provider user interface! |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
6 |
|
protected function getDefaultScopes() |
106
|
|
|
{ |
107
|
6 |
|
return $this->defaultScopes; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Check a provider response for errors. |
112
|
|
|
* |
113
|
|
|
* @throws IdentityProviderException |
114
|
|
|
* @param ResponseInterface $response |
115
|
|
|
* @param string $data Parsed response data |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
12 |
|
protected function checkResponse(ResponseInterface $response, $data) |
119
|
|
|
{ |
120
|
12 |
|
if (isset($data['error'])) { |
121
|
3 |
|
throw new IdentityProviderException( |
122
|
3 |
|
$data['error_description'] ?: $response->getReasonPhrase(), |
123
|
5 |
|
$response->getStatusCode(), |
124
|
1 |
|
$response |
|
|
|
|
125
|
2 |
|
); |
126
|
|
|
} |
127
|
9 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Generate a user object from a successful user details request. |
131
|
|
|
* |
132
|
|
|
* @param array $response |
133
|
|
|
* @param AccessToken $token |
134
|
|
|
* @return League\OAuth2\Client\Provider\ResourceOwnerInterface |
135
|
|
|
*/ |
136
|
6 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
137
|
|
|
{ |
138
|
6 |
|
return new LinkedInResourceOwner($response); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns the requested fields in scope. |
143
|
|
|
* |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
3 |
|
public function getFields() |
147
|
|
|
{ |
148
|
3 |
|
return $this->fields; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Updates the requested fields in scope. |
153
|
|
|
* |
154
|
|
|
* @param array $fields |
155
|
|
|
* |
156
|
|
|
* @return League\OAuth2\Client\Provider\LinkedIn |
157
|
|
|
*/ |
158
|
3 |
|
public function withFields(array $fields) |
159
|
|
|
{ |
160
|
3 |
|
$this->fields = $fields; |
161
|
|
|
|
162
|
3 |
|
return $this; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: