使用凭据提供商和AWSMobileClient检索Cognito身份ID

时间:2019-08-23 09:12:28

标签: ios swift authentication amazon-cognito

为什么我从凭证提供程序使用getIdentityID时为什么成功获取Cognito ID,但是在使用AWSMobileClient时却收到错误消息?

我已经与自定义开发人员提供商建立了联合身份。我正在使用由Amazon Cognito控制台生成的代码片段初始化Amazon Cognito凭据提供程序。

let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "IDENTITY_POOL_ID")
let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration

在此之后,我使用以下方法检索cognito身份ID:

credentialsProvider.getIdentityId().continueWith(block: { (task) -> AnyObject? in
    if (task.error != nil) {
        print("Error: " + task.error!.localizedDescription)
    }
    else {
        // the task result will contain the identity id
        let cognitoId = task.result!
        print("Cognito id: \(cognitoId)")
    }
    return task;
})

运行此命令时,我成功获取了身份ID,也可以在控制台上以未经身份验证的用户身份查看该身份ID。但是,当我尝试使用AWSMobileClient检索身份ID时,如下所示:

AWSMobileClient.sharedInstance().initialize { (userState, error) in
            if let userState = userState {
                print("UserState (AccountView): \(userState.rawValue)")
            } else if let error = error {
                print("error: \(error.localizedDescription)")
            }
        }

        // Retrieve your Amazon Cognito ID
AWSMobileClient.sharedInstance().getIdentityId().continueWith { task in
                    if let error = task.error {
                        print("error: \(error.localizedDescription) \((error as NSError).userInfo)")
                        print(error)
                    }
                    if let result = task.result {
                        print("identity id: \(result)")
                    }
                    return nil
                }

我收到AWSMobileClient错误30:错误:cognitoIdentityPoolNotConfigured(消息:“由于无法使用认知凭证,因此无法获取identityId。”)

这是为什么?我想念什么?

我正在使用 AWSMobileClient,AWSCore

环境(请填写以下信息):  -SDK版本:“ AWSMobileClient”,“〜> 2.10.0”,“ AWSCore”,“〜> 2.10.0”  -依赖性经理:Cocoapods  -Swift版本:5.0  -Xcode版本:10.3

设备信息(请完成以下信息):  -设备:MacOS 10.14.5  -iOS 12.4

0 个答案:

没有答案