在移动应用程序中配置AWS DynamoDB时获取身份验证错误

时间:2017-06-04 18:03:01

标签: ios swift amazon-web-services amazon-dynamodb amazon-cognito

因此,我尝试使用Amazon的DynamoDB Object Mapper将代码映射到已包含主键和特定属性的预先存在的数据库表中。我正在开发一个IOS应用程序。

当我尝试使用Object Mapper时,我收到以下错误:

The request failed. Error: Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=0 "(null)" UserInfo={__type=com.amazon.coral.service#MissingAuthenticationTokenException, message=Request is missing Authentication Token}

这个错误对我来说很奇怪,因为每当用户使用他们的电子邮件地址登录我的应用程序时,用户都会在用户池和身份池中正确显示。这似乎告诉我,我在Amazon Cognito和我的应用程序之间链接的池ID,身份池ID和应用客户端ID应该没有问题。

以下是我用于配置用户池和身份池的代码(在app delegate中配置):

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    // Setup Service Configuration
    let serviceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: nil)
    AWSServiceManager.default().defaultServiceConfiguration = serviceConfiguration

    // Link app to AWS user pool
    let userPoolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: CLIENT_ID, clientSecret: nil, poolId: USER_POOL_ID)
    AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: userPoolConfiguration, forKey: USER_POOL_ID)
    pool = AWSCognitoIdentityUserPool(forKey: USER_POOL_ID)

    // Configure the identity pool to connect to all AWS resources
    credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: IDENTITY_POOL_ID, identityProviderManager: pool)
    let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialsProvider)
    AWSServiceManager.default().defaultServiceConfiguration = configuration

    return true
}

当我尝试使用下面的DynamoDBObjectMapper的save函数时,我收到错误:

    @IBAction func login(_ sender: Any) {

    guard let confirmationCodeValue = confirmationCode.text, !confirmationCodeValue.isEmpty else {
        let alert = UIAlertController(title: "Confirmation code missing.",
                                      message: "Please enter a valid confirmation code.",
                                      preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        self.present(alert, animated: true, completion:nil)
        return
    }

    self.user?.confirmSignUp(confirmationCodeValue, forceAliasCreation: true).continueWith(block: {[weak self] (task: AWSTask) -> AnyObject? in
        guard let strongSelf = self else { return nil }
        DispatchQueue.main.async(execute: {
            if let error = task.error as NSError? {
                let alert = UIAlertController(title: "Error",
                                              message:error.userInfo["message"] as? String,
                                              preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
                strongSelf.present(alert, animated: true, completion:nil)
            }
            else {
                let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.default()
                let artistInformation = ArtistInformation()

                artistInformation?.UserID = self?.userID
                artistInformation?.ArtistName = UserDefaults.standard.string(forKey: "ArtistName")
                artistInformation?.ProfileType = UserDefaults.standard.string(forKey: "ProfileType")
                artistInformation?.CityYouRep = UserDefaults.standard.string(forKey: "CityYouRep")
                artistInformation?.Genre = UserDefaults.standard.string(forKey: "Genre")

                dynamoDBObjectMapper.save(artistInformation!).continueWith(block: { (task: AWSTask<AnyObject>!) -> Any? in
                    if let error = task.error as NSError? {
                        print("The request failed. Error: \(error)")
                    } else {
                        let alert = UIAlertController(title: "Registration Complete",
                                                      message: "Registration was successful.",
                                                      preferredStyle: .alert)

                        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: {(action) in
                            let _ = self?.navigationController?.popToRootViewController(animated: true)
                        }))
                        strongSelf.present(alert, animated: true, completion: nil)
                    }
                    return nil
                })

为了避免混淆,&#34; ArtistInformation&#34; class是一个链接到DynamoDB中现有表的类:

class ArtistInformation: AWSDynamoDBObjectModel, AWSDynamoDBModeling {

var UserID: String?
var ArtistName: String?
var ProfileType: String?
var CityYouRep: String?
var Genre: String?

class func dynamoDBTableName() -> String {
    return "Artist_Information"
}

class func hashKeyAttribute() -> String {
    return "userID"
}

}

这个错误的原因似乎是什么?如果您需要更多信息,请与我们联系。

0 个答案:

没有答案