protected function getAccessTokenFromCode(LinkedInUrlGeneratorInterface $urlGenerator, $code)
{
if (empty($code)) {
throw new LinkedInException('Could not get access token: The code was empty.');
}
$redirectUri = $this->getStorage()->get('redirect_uri');
try {
$url = $urlGenerator->getUrl('www', 'oauth/v2/accessToken');
$headers = ['Content-Type' => 'application/x-www-form-urlencoded'];
$body = http_build_query(['grant_type' => 'authorization_code', 'code' => $code, 'redirect_uri' => $redirectUri, 'client_id' => $this->appId, 'client_secret' => $this->appSecret]);
$response = ResponseConverter::convertToArray($this->getRequestManager()->sendRequest('POST', $url, $headers, $body));
} catch (LinkedInTransferException $e) {
// most likely that user very recently revoked authorization.
// In any event, we don't have an access token, so throw an exception.
throw new LinkedInException('Could not get access token: The user may have revoked the authorization response from LinkedIn.com was empty.', $e->getCode(), $e);
}
if (empty($response)) {
throw new LinkedInException('Could not get access token: The response from LinkedIn.com was empty.');
}
$tokenData = array_merge(['access_token' => null, 'expires_in' => null], $response);
$token = new AccessToken($tokenData['access_token'], $tokenData['expires_in']);
if (!$token->hasToken()) {
throw new LinkedInException('Could not get access token: The response from LinkedIn.com did not contain a token.');
}
return $token;
}