1
我正在嘗試爲我的移動應用使用AWS Cognito(用戶池)和AWS DynamoDB。如何通過認證的認證用戶(用戶池)在swift中訪問DynamoDB
我做了以下內容:
- 在AWS上Cognito創建用戶池。
- 在AWS Cognito上創建標識池,並在身份驗證提供程序上將用戶池標識,應用程序客戶端標識設置爲Cognito。
- 在AWS DynamoDB上創建SampleTable。
- 設置權限已通過身份驗證的角色以訪問AWS IAM上的SampleTable。
我創造了這個代碼:
let userPoolConfigration = AWSServiceConfiguration.init(region: AWSRegionType.USEast1, credentialsProvider: nil)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = userPoolConfigration
let userServiceConfigration = AWSCognitoIdentityUserPoolConfiguration.init(clientId: "(client id from user pool)", clientSecret: "(client secret from user pool)", poolId: "(user pool id from user pool)")
AWSCognitoIdentityUserPool.registerCognitoIdentityUserPoolWithUserPoolConfiguration(userServiceConfigration, forKey: "AmazonCognitoIdentityProvider")
let pool = AWSCognitoIdentityUserPool(forKey: "AmazonCognitoIdentityProvider")
let user = pool.getUser("sampleuser")
user.getSession("sampleuser", password: "samplepassword", validationData: nil, scopes: nil).continueWithBlock({ task in
if (task.error == nil) {
let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.USEast1,
identityPoolId:"(identity id from identity pool)")
let ret = task.result as! AWSCognitoIdentityUserSession
let logins: NSDictionary = NSDictionary(dictionary: ["cognito-idp.us-east-1.amazonaws.com/(user pool id from user pool)" : ret.idToken!.tokenString])
credentialsProvider.logins = logins as [NSObject : AnyObject]
let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
credentialsProvider.clearKeychain()
credentialsProvider.credentials().continueWithBlock {(task: AWSTask!) -> AnyObject! in
let result = task.result as! AWSCredentials
let newcredentialsProvider = AWSStaticCredentialsProvider(accessKey:result.accessKey, secretKey: result.secretKey)
let newdefaultServiceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: newcredentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = newdefaultServiceConfiguration
let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.defaultDynamoDBObjectMapper()
let scanExpression = AWSDynamoDBScanExpression()
dynamoDBObjectMapper.scan(SampleTable.self, expression: scanExpression).continueWithBlock({ task in
print(task.result)
return nil
})
return nil
}
}
return nil
})
但我得到這個錯誤時, 「dynamoDBObjectMapper.scan()」 已運行:
2016-06-23 16:57:04.988 AWSiOSSDK v2.4.3 [Debug] AWSURLSessionManager.m line:525 | -[AWSURLSessionManager printHTTPHeadersAndBodyForRequest:] | Request headers:
{
"Content-Type" = "application/x-amz-json-1.0";
Host = "dynamodb.us-east-1.amazonaws.com";
"User-Agent" = "aws-sdk-iOS/2.4.3 iPhone-OS/9.2 en_US mapper";
"X-Amz-Date" = 20160623T075704Z;
"X-Amz-Target" = "DynamoDB_20120810.Scan";
}
2016-06-23 16:57:04.988 if-she[3704:367618] AWSiOSSDK v2.4.3 [Debug] AWSURLSessionManager.m line:542 | -[AWSURLSessionManager printHTTPHeadersAndBodyForRequest:] | Request body:
{"TableName":"SampleTable"}
2016-06-23 16:57:05.844 AWSiOSSDK v2.4.3 [Debug] AWSURLSessionManager.m line:552 | -[AWSURLSessionManager printHTTPHeadersForResponse:] | Response headers:
{
Connection = "keep-alive";
"Content-Length" = 125;
"Content-Type" = "application/x-amz-json-1.0";
Date = "Thu, 23 Jun 2016 07:57:05 GMT";
Server = Server;
"x-amz-crc32" = 2088342776;
"x-amzn-RequestId" = FPM78JRBQIKG2DOLI62SL4IRMFVV4KQNSO5AEMVJF66Q9ASUAAJG;
}
2016-06-23 16:57:05.845 AWSiOSSDK v2.4.3 [Debug] AWSURLResponseSerialization.m line:63 | -[AWSJSONResponseSerializer responseObjectForResponse:originalRequest:currentRequest:data:error:] | Response body:
{"__type":"com.amazon.coral.service#MissingAuthenticationTokenException","message":"Request is missing Authentication Token"}
也許我缺少了如何使用身份驗證令牌。 我的代碼有什麼問題?
謝謝。
謝謝您的回覆。 AWSStaticCredentialsProvider的初始化方法沒有參數sessionKey。 因此,我刪除代碼「初始化AWSStaticCredentialsProvider」和「註冊defaultServiceConfiguration它」 但我得到了同樣的錯誤。 – hiro