jambtc

cognito code to token 1

Feb 28th, 2023
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.98 KB | None | 0 0
  1. public function exchangeAuthorizationCodeForToken($code)
  2.     {
  3.         $token_url = $this->domain_url ."/oauth2/token";
  4.         $params = array(
  5.             'grant_type' => 'authorization_code',
  6.             'client_id' => $this->client_id,
  7.             'code' => $code,
  8.             'redirect_uri' => $this->redirect_uri,
  9.         );
  10.         $headers = [];
  11.         $headers[] = 'Content-Type: application/x-www-form-urlencoded';
  12.         $headers[] = 'Authorization: Basic ' . base64_encode($this->client_id . ':' . Yii::$app->params['aws']['client_secret']);
  13.          
  14.         $ch = curl_init();
  15.         curl_setopt($ch, CURLOPT_URL, $token_url);
  16.         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  17.         curl_setopt($ch, CURLOPT_POST, true);
  18.         curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
  19.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  20.         $response = curl_exec($ch);
  21.         curl_close($ch);
  22.         return json_decode($response, true);
  23.     }
Add Comment
Please, Sign In to add comment