Skip to content

Commit 2135cc6

Browse files
committed
Add support of optional oauth2_access_token parameter
1 parent 401d74a commit 2135cc6

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

src/Client.php

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ class Client
4242
const OAUTH2_RESPONSE_TYPE = 'code';
4343

4444
/**
45+
* Client Id
4546
* @var string
4647
*/
4748
protected $clientId;
4849

4950
/**
51+
* Client Secret
5052
* @var string
5153
*/
5254
protected $clientSecret;
@@ -95,6 +97,32 @@ class Client
9597
*/
9698
protected $oAuthApiRoot = self::OAUTH2_API_ROOT;
9799

100+
/**
101+
* Use oauth2_access_token parameter instead of Authorization header
102+
*
103+
* @var bool
104+
*/
105+
protected $useTokenParam = false;
106+
107+
/**
108+
* @return bool
109+
*/
110+
public function isUsingTokenParam()
111+
{
112+
return $this->useTokenParam;
113+
}
114+
115+
/**
116+
* @param bool $useTokenParam
117+
*
118+
* @return Client
119+
*/
120+
public function setUseTokenParam($useTokenParam)
121+
{
122+
$this->useTokenParam = $useTokenParam;
123+
return $this;
124+
}
125+
98126
/**
99127
* List of default headers
100128
*
@@ -266,13 +294,12 @@ public function getAccessToken($code = '')
266294
$json = self::responseToArray(
267295
$requestException->getResponse()
268296
);
269-
$lnException = new Exception(
297+
throw new Exception(
270298
$requestException->getMessage(),
271299
$requestException->getCode(),
272300
$requestException,
273301
static::extractErrorDescription($json)
274302
);
275-
throw $lnException;
276303
}
277304
$json = self::responseToArray($response);
278305
$this->setAccessToken(
@@ -291,9 +318,10 @@ public function getAccessToken($code = '')
291318
*/
292319
protected static function responseToArray($response)
293320
{
294-
$body = $response->getBody();
295-
$content = $body->getContents();
296-
return \GuzzleHttp\json_decode($content, true);
321+
return \GuzzleHttp\json_decode(
322+
$response->getBody()->getContents(),
323+
true
324+
);
297325
}
298326

299327
/**
@@ -382,9 +410,8 @@ public function setState($state)
382410
* @return string
383411
*/
384412
public function getLoginUrl(
385-
array $scope = ['r_basicprofile', 'r_emailaddress']
413+
array $scope = [Scope::READ_BASIC_PROFILE, Scope::READ_EMAIL_ADDRESS]
386414
) {
387-
388415
$params = [
389416
'response_type' => self::OAUTH2_RESPONSE_TYPE,
390417
'client_id' => $this->getClientId(),
@@ -465,14 +492,17 @@ protected function buildUrl($endpoint, $params)
465492
public function api($endpoint, array $params = [], $method = Method::GET)
466493
{
467494
$headers = $this->getDefaultApiHeaders();
468-
$headers['Authorization'] = 'Bearer ' . $this->accessToken->getToken();
495+
$uri = $endpoint;
496+
$options = [];
497+
if ($this->isUsingTokenParam()) {
498+
$params['oauth2_access_token'] = $this->accessToken->getToken();
499+
} else {
500+
$headers['Authorization'] = 'Bearer ' . $this->accessToken->getToken();
501+
}
469502
$guzzle = new GuzzleClient([
470503
'base_uri' => $this->getApiRoot(),
471504
'headers' => $headers,
472505
]);
473-
$uri = $endpoint;
474-
$options = [];
475-
//$params['oauth2_access_token'] = $this->accessToken->getToken();
476506
switch ($method) {
477507
case Method::GET:
478508
if (!empty($params)) {
@@ -497,13 +527,12 @@ public function api($endpoint, array $params = [], $method = Method::GET)
497527
$json = self::responseToArray(
498528
$requestException->getResponse()
499529
);
500-
$lnException = new Exception(
530+
throw new Exception(
501531
$requestException->getMessage(),
502532
$requestException->getCode(),
503533
$requestException,
504534
static::extractErrorDescription($json)
505535
);
506-
throw $lnException;
507536
}
508537
return self::responseToArray($response);
509538
}
@@ -531,6 +560,7 @@ private static function extractErrorDescription($json)
531560
* @param array $params
532561
*
533562
* @return array
563+
* @throws \LinkedIn\Exception
534564
*/
535565
public function get($endpoint, array $params = [])
536566
{
@@ -544,6 +574,7 @@ public function get($endpoint, array $params = [])
544574
* @param array $params
545575
*
546576
* @return array
577+
* @throws \LinkedIn\Exception
547578
*/
548579
public function post($endpoint, array $params = [])
549580
{

0 commit comments

Comments
 (0)