1
私のモバイルアプリケーションにAWS Cognito(ユーザープール)とAWS DynamoDBを使用しようとしています。認証済みのCognitoユーザー(ユーザープール)によるDynamoDBへの迅速なアクセス方法
私は次のようでした:
- がAWS Cognito上のユーザープールを作成します。
- AWS CognitoでIDプールを作成し、ユーザープールID、アプリクライアントIDを認証プロバイダの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のinitializeメソッドにsessionKeyパラメータがありません。 私はコードを削除 "AWSStaticCredentialsProviderを初期化"と "regist defaultServiceConfigurationそれ" しかし、私は同じエラーがあった。 – hiro