2017-04-19 80 views
0

爲cognito用戶池的文檔可以在這裏找到:我可以使用ListUsers API通過用戶的uuid查詢Cognito用戶池嗎?

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

在這一點,他們不說你是否可以通過自動生成的子屬性,它是一個UUID查詢用戶。它明確表示不能通過自定義屬性搜索用戶,但sub/uuid不是自定義屬性。奇怪的是,在可搜索屬性列表中,sub/uuid不是其中之一。當然,雖然你可以通過他們的UUID查找用戶,但是如何做到這一點?

回答

1

你知道,我已經使用了COgnito,但是從來不需要通過子查詢(或者除了用戶名之外的其他參數)查找。我研究它,因爲你可以,但它不是很清楚(就像他們的許多文檔)。這是我看到你可以嘗試...希望它可以幫助男人。

 // 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 
+0

用戶非常感謝你對這個答案,似乎很樂於助人,在與他們的文檔線。奇怪的是,雖然我成功地導入了ListUsersRequest庫,但是我的eclipse告訴我.withFilter不是ListUsersRequest類的廢話方法。我有困惑我正在使用正確的ListUsersRequest類,並從正確的地方導入它,任何想法,爲什麼它不會工作? – HelloCoding

+0

另外我想知道在這段代碼中identityUserPoolProviderClient是什麼類?我檢查了AmazonCognitoIdentityClient類的文檔,但它沒有方法listUsers,所以我不認爲是這樣。 – HelloCoding

+0

嘿,很高興幫助。這裏是一些更多的信息添加到答案... – Matt

相關問題