Advertisement
jambtc

cognito code to token 2

Feb 28th, 2023
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.34 KB | None | 0 0
  1. public function exchangeAuthorizationCodeForToken($code)
  2.     {
  3.         $client = new CognitoIdentityProviderClient([
  4.             'version' => Yii::$app->params['aws']['version'],
  5.             'region' => Yii::$app->params['aws']['region'],
  6.             'credentials' => [
  7.                 'key' => Yii::$app->params['aws']['credentials']['key'],
  8.                 'secret' => Yii::$app->params['aws']['credentials']['secret'],
  9.             ]
  10.         ]);
  11.  
  12.         try {
  13.             $result = $client->exchangeAuthorizationCode([
  14.                 'ClientId' => $this->client_id,
  15.                 'ClientSecret' => Yii::$app->params['aws']['client_secret'],
  16.                 'RedirectUri' => $this->redirect_uri,
  17.                 'Code' => $code,
  18.             ]);
  19.  
  20.             if (isset($result['AccessToken'])) {
  21.                 // Access token obtained, use it to retrieve user object
  22.                 $accessToken = $result['AccessToken'];
  23.  
  24.                 return $accessToken;
  25.  
  26.                
  27.             } else {
  28.                 // Error exchanging authorization code for access token
  29.                 echo 'Error exchanging authorization code for access token';
  30.             }
  31.         } catch (CognitoIdentityProviderException $e) {
  32.             // Error exchanging authorization code for access token
  33.             echo $e->getMessage();
  34.         }
  35.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement