Happyr\LinkedIn\Authenticator::getAccessTokenFromCode PHP Method

getAccessTokenFromCode() protected method

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.
protected getAccessTokenFromCode ( Happyr\LinkedIn\Http\LinkedInUrlGeneratorInterface $urlGenerator, string $code ) : Happyr\LinkedIn\AccessToken
$urlGenerator Happyr\LinkedIn\Http\LinkedInUrlGeneratorInterface
$code string An authorization code.
return Happyr\LinkedIn\AccessToken An access token exchanged for the authorization code.
    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;
    }