/** * 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; }