Sample code to log into Amazon Cognito and get Access Token [CLI curl edition]

Tadashi Shigeoka ·  Thu, October 27, 2022

I’ll introduce sample code to log into Amazon Cognito and get an Access Token.

AWS

AWS_COGNITO_CLIENT_ID=your_client_id
[email protected]
AWS_COGNITO_PASSWORD="input your password"

curl -s 
-XPOST 'https://cognito-idp.ap-northeast-1.amazonaws.com/' 
-H 'x-amz-target: AWSCognitoIdentityProviderService.InitiateAuth' 
-H 'content-type: application/x-amz-json-1.1' --data-raw '{"AuthFlow":"USER_PASSWORD_AUTH","ClientId":"'${AWS_COGNITO_CLIENT_ID}'","AuthParameters":{"USERNAME":"'${AWS_COGNITO_USERNAME}'","PASSWORD":"'${AWS_COGNITO_PASSWORD}'"},"ClientMetadata":{}}' | 
jq -r .AuthenticationResult.AccessToken

That’s all from the Gemba about logging into Amazon Cognito with CLI and getting an Access Token.