使用ListUsers API或iOS SDK搜索用户Amazon Cognito

时间:2016-12-28 15:09:03

标签: ios amazon-web-services aws-sdk amazon-cognito aws-cognito

我正在Swift中创建一个iOS应用程序,但我无法找到一种方法来搜索或获取带有API的Cognito用户列表。根据亚马逊文档,它说要使用ListUsers API;但是,它没有提供端点来发出请求,而且使用AWS验证REST API请求相当困难,所以无论如何都要通过iOS SDK来实现这一点吗?这些是我想要包含在我的请求中的参数。

[
        "AttributesToGet": ["username" ],
        "Filter": "username ^= \"micheal\"",
        "Limit": 10,
        "UserPoolId": "\(AWSCognitoUserPoolId)"
    ]

3 个答案:

答案 0 :(得分:2)

您必须搜索Mobile SDK。

适用于iOS

 // Make a AWSCognitoListUsers Request
    let getUsersRequest = AWSCognitoIdentityProviderListUsersRequest()

    // Add the Parameters
    //getUsersRequest?.attributesToGet = ["username"]
    getUsersRequest?.userPoolId = AWSCognitoUserPoolId

    // Make the Request
    AWSCognitoIdentityProvider(forKey: AWSCognitoUserPoolId).listUsers(getUsersRequest!, completionHandler: { (response, error) in


        // The response variable contains the Cognito Response

    })

答案 1 :(得分:0)

有一个宁静的API端点可以做到这一点,因为使用aws CLI可以做到这一点。 Command Line List User Pool

aws CLI是检查接口是否可以执行此操作的好方法。

如何在IOS SDK中执行此操作是另一回事。 restful命令集和SDK方法名之间几乎没有关系(无)。我认为这是你要求CognitoIdentityProvider做的事情......我认为它是listUsers或者左右,我想你也可以列出这些池。

+1这个

的另一个答案

答案 2 :(得分:0)

这是从 Cognito 获取用户列表的工作代码。

//IAM User Key and Secret Key Required
let IAMUserKey = “****”
let IAMSecretKey = “****”

//Set Credentials
let credentialsProvider : AWSStaticCredentialsProvider = AWSStaticCredentialsProvider(accessKey: IAMUserKey, secretKey: IAMSecretKey)
let serviceConfiguration: AWSServiceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialsProvider)
        
//Register Identity Provider
AWSCognitoIdentityProvider.register(with: serviceConfiguration, forKey:UserPoolID)
        
//Request
let getUsersRequest = AWSCognitoIdentityProviderListUsersRequest()
getUsersRequest?.userPoolId = UserPoolID
//mentioned name of attributes in array - name, given_name, username, email and so on
getUsersRequest?.attributesToGet = ["name"]
//Add Value in filter you want to search 
getUsersRequest?.filter = "name = \”jen\””
//It will search string in including string
//getUsersRequest?.filter = "name ^= \"jen\""
   
AWSCognitoIdentityProvider(forKey: UserPoolID).listUsers(getUsersRequest!,completionHandler: {(response, error) in
  print("response:\(String(describing: response))")
  print("error:\(String(describing: error))")
})