PayPal\Auth\Openid\PPOpenIdTokeninfo::createFromAuthorizationCode PHP Method

createFromAuthorizationCode() public static method

Creates an Access Token from an Authorization Code.
public static createFromAuthorizationCode ( array $params, $clientId, $clientSecret, PPApiContext $apiContext = null ) : PPOpenIdTokeninfo
$params array (allowed values are client_id, client_secret, grant_type, code and redirect_uri) (required) client_id from developer portal (required) client_secret from developer portal (required) code is Authorization code previously received from the authorization server (required) redirect_uri Redirection endpoint that must match the one provided during the authorization request that ended in receiving the authorization code. (optional) grant_type is the Token grant type. Defaults to authorization_code
$apiContext PayPal\Common\PPApiContext Optional API Context
return PPOpenIdTokeninfo
    public static function createFromAuthorizationCode($params, $clientId, $clientSecret, $apiContext = null)
    {
        static $allowedParams = array('grant_type' => 1, 'code' => 1, 'redirect_uri' => 1);
        if (is_null($apiContext)) {
            $apiContext = new PPApiContext();
        }
        $config = $apiContext->getConfig();
        if ($apiContext->get($clientId) !== false) {
            $clientId = $apiContext->get($clientId);
        }
        if ($apiContext->get($clientSecret) !== false) {
            $clientSecret = $apiContext->get($clientSecret);
        }
        if (!array_key_exists('grant_type', $params)) {
            $params['grant_type'] = 'authorization_code';
        }
        $call = new PPRestCall($apiContext);
        $token = new PPOpenIdTokeninfo();
        $token->fromJson($call->execute(array(new PPOpenIdHandler()), "/v1/identity/openidconnect/tokenservice", "POST", http_build_query(array_intersect_key($params, $allowedParams)), array('Content-Type' => 'application/x-www-form-urlencoded', 'Authorization' => 'Basic ' . base64_encode($clientId . ":" . $clientSecret))));
        return $token;
    }

Usage Example

 /**
  * @t1est
  */
 public function t1estOperations()
 {
     $clientId = 'AQkquBDf1zctJOWGKWUEtKXm6qVhueUEMvXO_-MCI4DQQ4-LWvkDLIN2fGsd';
     $clientSecret = 'ELtVxAjhT7cJimnz5-Nsx9k2reTKSVfErNQF-CmrwJgxRtylkGTKlU4RvrX';
     $params = array('code' => '<FILLME>', 'redirect_uri' => 'https://devtools-paypal.com/', 'client_id' => $clientId, 'client_secret' => $clientSecret);
     $accessToken = PPOpenIdTokeninfo::createFromAuthorizationCode($params);
     $this->assertNotNull($accessToken);
     $params = array('refresh_token' => $accessToken->getRefreshToken(), 'client_id' => $clientId, 'client_secret' => $clientSecret);
     $accessToken = $accessToken->createFromRefreshToken($params);
     $this->assertNotNull($accessToken);
 }