Closed jordandobrev closed 4 years ago
Meanwhile, you can do this:
New class on your project
<?php
namespace XXXX\Services;
use OAuth\OAuth2\Token\StdOAuth2Token;
use OAuth\OAuth2\Service\AbstractService;
use OAuth\Common\Http\Exception\TokenResponseException;
use OAuth\Common\Http\Uri\Uri;
use OAuth\Common\Consumer\CredentialsInterface;
use OAuth\Common\Http\Client\ClientInterface;
use OAuth\Common\Storage\TokenStorageInterface;
use OAuth\Common\Http\Uri\UriInterface;
/**
* Linkedin Oauth definition for the lusitanian/oauth library.
* (The provided Linkedin Oauth lib is outdated)
*/
class LinkedInOauthDefinition extends AbstractService
{
/**
* Defined scopes
*/
const SCOPE_R_LITEPROFILE = 'r_liteprofile';
const SCOPE_R_EMAILADDRESS = 'r_emailaddress';
const SCOPE_W_MEMBER_SOCIAL = 'w_member_social';
public function __construct(
CredentialsInterface $credentials,
ClientInterface $httpClient,
TokenStorageInterface $storage,
$scopes = array(),
UriInterface $baseApiUri = null
) {
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
if (null === $baseApiUri) {
$this->baseApiUri = new Uri('https://api.linkedin.com/v2/');
}
}
/**
* {@inheritdoc}
*/
public function getAuthorizationEndpoint()
{
return new Uri('https://www.linkedin.com/oauth/v2/authorization');
}
/**
* {@inheritdoc}
*/
public function getAccessTokenEndpoint()
{
return new Uri('https://www.linkedin.com/oauth/v2/accessToken');
}
/**
* {@inheritdoc}
*/
protected function getAuthorizationMethod()
{
return static::AUTHORIZATION_METHOD_HEADER_BEARER;
}
/**
* {@inheritdoc}
*/
protected function parseAccessTokenResponse($responseBody)
{
$data = json_decode($responseBody, true);
if (null === $data || !is_array($data)) {
throw new TokenResponseException('Unable to parse response.');
} elseif (isset($data['error'])) {
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
}
$token = new StdOAuth2Token();
$token->setAccessToken($data['access_token']);
$token->setLifeTime($data['expires_in']);
if (isset($data['refresh_token'])) {
$token->setRefreshToken($data['refresh_token']);
unset($data['refresh_token']);
}
unset($data['access_token']);
unset($data['expires_in']);
$token->setExtraParams($data);
return $token;
}
}
Then
// Register our own Linkedin Oauth Provider (the one from the service is obsolete)
$serviceFactory->registerService("linkedin-apiv2", \XXXX\Services\LinkedInOauthDefinition::class);
// Create the Oauth service
$linkedinService = $serviceFactory->createService('linkedin-apiv2', $credentials, $storage, array('r_liteprofile', 'r_emailaddress'));
// This was a callback request from linkedin, get the token
$token = $linkedinService->requestAccessToken($code, $state);
// Now fetch the user infos
$result = json_decode($linkedinService->request('/me'), true);
thanks @nand2 it helped a lot!
I was looking for a workaround with the param $baseUri of createService
, it worked to override to v2 the uri but the scopes also changed, so I was stuck...
So thanks for your good idea!
LinkedIn is deprecating the v1 api. Any plans to update the LinkedIn driver so it works with the latest v2 api?