我可以使用ListUsers API通过用户的uuid查询Cognito用户池吗?

时间:2017-04-19 20:49:50

标签: java amazon-web-services aws-cognito

可以在此处找到Cognito用户池的文档:

http://docs.aws.amazon.com/cognito/latest/developerguide/how-to-manage-user-accounts.html

在这里他们没有说你是否可以通过自动生成的子属性查询用户,这是一个uuid。它明确表示您无法通过自定义属性搜索用户,但sub / uuid不是自定义属性。奇怪的是,在可搜索属性列表中,sub / uuid不是其中之一。当然,虽然你可以通过他们的UUID查找用户,但是如何做到这一点?

1 个答案:

答案 0 :(得分:2)

你知道,我使用过COgnito但从不需要通过sub(或用户名以外的其他参数)查找。我调查了它,因为肯定你可以,但它不是很清楚(就像他们的很多文档)。这是我看到你可以尝试的...希望它能帮助人类。

        // the imported ListUsersResult is...
        import com.amazonaws.services.cognitoidp.model.ListUsersRequest;
        import com.amazonaws.services.cognitoidp.model.ListUsersResult;

             // class var

         protected final AWSCognitoIdentityProviderClient identityUserPoolProviderClient;

        // omitted stuff... 
        // initialize the Cognito Provider client.  This is used to talk to the user pool
        identityUserPoolProviderClient = new AWSCognitoIdentityProviderClient(new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY)); // creds are loaded via variables that are supplied to my program dynamically
        identityUserPoolProviderClient.setRegion(RegionUtils.getRegion(USER_POOL_REGION)); // var loaded 


        // ...some code omitted        
        ListUsersRequest listUsersRequest = new ListUsersRequest();
        listUsersRequest.withUserPoolId(USER_POOL_ID); // id of the userpool, look this up in Cognito console
        listUsersRequest.withFilter("sub=xyz");  // i THINK this is how the Filter works... the documentation is terribad


    // get the results 
    ListUsersResult result = identityUserPoolProviderClient.listUsers(listUsersRequest); 

    List<UserType> userTypeList = result.getUsers();
    // loop through them
    for (UserType userType : userTypeList) {
        List<AttributeType> attributeList = userType.getAttributes();
        for (AttributeType attribute : attributeList) {
            String attName = attribute.getName();
            String attValue = attribute.getValue();
            System.out.println(attName + ": " + attValue);
        }
    }

如果你有用户名,你就可以得到像这样的用户

    // build the request
    AdminGetUserRequest idRequest = new AdminGetUserRequest();
    idRequest.withUserPoolId(USER_POOL_ID);
    idRequest.withUsername(username);

    // call cognito for the result
    AdminGetUserResult result = identityUserPoolProviderClient.adminGetUser(idRequest);
    // loop through results