Get client data through AWS cognito authuntication code (PHP)

0

Hi,

My client portal is written in PHP 7.3 and access manage by Cognito. I need to get client details after successful login. I managed to get the access token and the id token using the authorization code. But, unsuccess in getting user data using those codes. I need recommended PHP code to obtain client data.

Regards,

Sanka

Sanka
preguntada hace 9 meses420 visualizaciones
2 Respuestas
0
Respuesta aceptada
Sanka
respondido hace 9 meses
0

Hi,

Recommended way to access AWS services with PHP is via AWS PHP SDK.

Specifically for Cognito: https://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-cognitoidentity.html

Available Cognito APIs: https://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-cognitoidentity.html#api-reference

ListIdentities and DescribeIdentity are the APIs that you need.

Best,

Didier

profile pictureAWS
EXPERTO
respondido hace 9 meses
  • Hi Didier,

    Thanks for the answer. The reference doesn't explain how to get the client data out. My code segment as follows.

    if ($responseBody!=null){ $tokenData = json_decode($responseBody, true); $accessToken = $tokenData['access_token']; $idToken = $tokenData['id_token']; error_log("----2 idToken : ".$idToken); error_log("----2 accessToken : ".$accessToken);

    if ($accessToken!=null){ $credentials = [ 'key' => $accessToken, 'secret' => $clientSecret, 'token' => $idToken, // Only needed for temporary session tokens (e.g., with AssumeRole) 'verify' => false, ]; $cognitoClient = new CognitoIdentityProviderClient([ 'version' => 'latest', 'region' => 'eu-west-2', 'credentials' => $credentials, 'verify' => false, ]);

    try {
      $user = $cognitoClient->getUser([
          'AccessToken' => $accessToken,
          'verify' => false,
      ]);
      error_log("----2 getUser 2 ");
      $userAttributes = $result['UserAttributes'];
      print_r($userAttributes);
    } catch (CognitoIdentityProviderException $e) {
      error_log("----2 getUser CognitoIdentityProviderException ");
      var_dump($e);
    }catch (Exception $e) {
      error_log("----2 getUser Exception ");
      var_dump($e);
    }
    

    -

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas