我可以使用 ListUsers API 通过用户的 uuid 查询 Cognito 用户池吗?
Can I use the ListUsers API to query a Cognito User Pool by a user's uuid?
可在此处找到 Cognito 用户池的文档:
http://docs.aws.amazon.com/cognito/latest/developerguide/how-to-manage-user-accounts.html
这里没有说是否可以通过自动生成的子属性查询用户,是uuid。它明确表示您不能通过自定义属性搜索用户,但 sub/uuid 不是自定义属性。奇怪的是,在可搜索属性列表中 sub/uuid 不是其中之一。当然,虽然您可以通过用户的 UUID 查找用户,但这将如何完成??
你知道,我使用过 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
可在此处找到 Cognito 用户池的文档:
http://docs.aws.amazon.com/cognito/latest/developerguide/how-to-manage-user-accounts.html
这里没有说是否可以通过自动生成的子属性查询用户,是uuid。它明确表示您不能通过自定义属性搜索用户,但 sub/uuid 不是自定义属性。奇怪的是,在可搜索属性列表中 sub/uuid 不是其中之一。当然,虽然您可以通过用户的 UUID 查找用户,但这将如何完成??
你知道,我使用过 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