首页 > 解决方案 > 使用 credentialsProvider 与 AWSMobileClient 检索 Cognito 身份 ID

问题描述

为什么我在使用凭证提供商的 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 Identity 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,并且我还可以在我的控制台上将其视为联合身份的未经身份验证的用户。但是,当我尝试使用 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,因为 cognito 凭证配置不可用。”)

为什么是这样?我错过了什么?

我正在使用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

标签: iosswiftauthenticationamazon-cognito

解决方案


推荐阅读