How do I obtain an ID token from Cognito without hosted UI

0

I'm developing an API that will be used by several companies in their IT landscape. I'm looking to use Cognito as user pool for authenticating API Gateway requests. I've created polls and API and have obtained an ID token in postman for proof-of-concept, but I can't seems to figure out how to get an ID token without using the hosted UI. As my customers developers will integrate with my API in their integration platforms, obtaining a token is something that must be possible without loggin in to a webpage.

2 個答案
2

Hello.

You can obtain an ID token from Amazon Cognito without using the hosted UI by performing the OAuth 2.0 token endpoint request. You can use the "password" grant type if you want to exchange a user's username and password for tokens directly.

Below are the steps to obtain an ID token using the AWS CLI and an HTTP request.

Using AWS CLI You can use the initiate-auth command in AWS CLI to initiate the authentication process.

  • Install AWS CLI: Make sure you have the AWS Command Line Interface installed.
  • Configure AWS CLI: Run aws configure to set your credentials and default region.

Run the initiate-auth Command:

aws cognito-idp initiate-auth \
  --auth-flow USER_PASSWORD_AUTH \
  --auth-parameters USERNAME=your_username,PASSWORD=your_password \
  --client-id your_app_client_id \
  --region your_aws_region

Using HTTP Request Install a Tool for Sending HTTP Requests: Install a tool like curl or use a platform like Postman.

curl -X POST \
  --url https://your_domain.auth.your_region.amazoncognito.com/oauth2/token \
  --header 'Content-Type: application/x-www-form-urlencoded' \
  --data-urlencode 'grant_type=password' \
  --data-urlencode 'client_id=your_app_client_id' \
  --data-urlencode 'username=your_username' \
  --data-urlencode 'password=your_password' \
  --data-urlencode 'scope=email openid'

Regards, Andrii

profile picture
專家
已回答 7 個月前
profile picture
專家
已審閱 9 天前
  • Hi Andrii! With your example, I only get "unsupported grant type". I can seems to find anything around a password grant type.

0

If you want to use OAuth you have to use hosted UI. Cognito supports grant types of : Authorization code grant, Implicit grant, Client credentials. It does not support Resource owner password credentials. However from your description you don't need/want to use OAuth. Refer to "User pool authentication flow" https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-authentication-flow.html to select an authentication flow that is suitable for your use case (the application that your customer develops).

AWS
已回答 7 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南