简体   繁体   中英

Amazon cognito unauthenticated flow from java

I am trying to get an unauthorized cognito token from a java code. Here is the code:

import com.amazonaws.services.cognitoidentity.AmazonCognitoIdentity;
import com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClientBuilder;
import com.amazonaws.services.cognitoidentity.model.GetCredentialsForIdentityRequest;
import com.amazonaws.services.cognitoidentity.model.GetIdRequest;
import com.amazonaws.services.cognitoidentity.model.GetIdResult;
import com.amazonaws.services.cognitoidentity.model.GetOpenIdTokenRequest;

public class unAuthorizedToken {
   public static void main(String args[]){
     AmazonCognitoIdentity identity =  AmazonCognitoIdentityClientBuilder.standard().withRegion("us-east-1").build();
    GetIdResult getId = identity.getId(new GetIdRequest().withIdentityPoolId("us-east-1:8xxxxxx-xxxx-xxxx-bxxx-5xxxxxxxxxxx"));

  //// obtain identity id and token to return to your client
    String identityId = getId.getIdentityId();
    System.out.println("IdentityId =" + identityId);
    GetOpenIdTokenRequest req = new GetOpenIdTokenRequest().withIdentityId(identityId);
    String token = identity.getOpenIdToken(req).getToken();
    System.out.println("Token =" + token);
}
}

The unauthenticated cognito token flow is working with javascript. but with Java I am getting the identityId but while getting the token, it is asking me to use enhanced flow at this line: String token = identity.getOpenIdToken(req).getToken(); Exception: Exception in thread "main" com.amazonaws.services.cognitoidentity.model.InvalidParameterException: Basic (classic) flow is not enabled, please use enhanced flow. (Service: AmazonCognitoIdentity; Status Code: 400; Error Code: InvalidParameterException; Request ID: 78a20c92-c0b0-4393-b972-8fa69765d7c1; Proxy: null) What is enhanced flow? does it mean unauthenticated access is not allowed from java? Please help me debug this code.

Also wanted to understand what is the difference between: com.amazonaws.services.* and software.amazon.awssdk.* libraries?

Ok. according to this link: https://docs.aws.amazon.com/cognito/latest/developerguide/authentication-flow.html tokenid is not needed. we can pass identity id for authentication

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM