/** * Retrieves an access token for the given authorization code * (previously generated from www.linkedin.com on behalf of * a specific user). The authorization code is sent to www.linkedin.com * and a legitimate access token is generated provided the access token * and the user for which it was generated all match, and the user is * either logged in to LinkedIn or has granted an offline access permission. * * @param LinkedInUrlGeneratorInterface $urlGenerator * @param string $code An authorization code. * * @return AccessToken An access token exchanged for the authorization code. * * @throws LinkedInException */ 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; }
/** * Retrieves an access token for the given authorization code * (previously generated from www.linkedin.com on behalf of * a specific user). The authorization code is sent to www.linkedin.com * and a legitimate access token is generated provided the access token * and the user for which it was generated all match, and the user is * either logged in to LinkedIn or has granted an offline access permission. * * @param string $code An authorization code. * @param string $redirectUri Where the user should be redirected after token is generated. * Default is the current url * * @return AccessToken|null An access token exchanged for the authorization code, or * null if an access token could not be generated. */ protected function getAccessTokenFromCode($code, $redirectUri = null) { if (empty($code)) { return; } if ($redirectUri === null) { $redirectUri = $this->getUrlGenerator()->getCurrentUrl(); } try { $response = $this->getRequest()->send('POST', $this->getUrlGenerator()->getUrl('www', 'uas/oauth2/accessToken'), ['body' => array('grant_type' => 'authorization_code', 'code' => $code, 'redirect_uri' => $redirectUri, 'client_id' => $this->getAppId(), 'client_secret' => $this->getAppSecret())]); } catch (LinkedInApiException $e) { // most likely that user very recently revoked authorization. // In any event, we don't have an access token, so say so. return; } if (empty($response)) { return; } $tokenData = array_merge(array('access_token' => null, 'expires_in' => null), $response); $token = new AccessToken($tokenData['access_token'], $tokenData['expires_in']); if (!$token->hasToken()) { return; } return $token; }