Ich versuche, AWS Cognito (User Pools) und AWS DynamoDB für meine mobilen Apps zu verwenden.Zugriff auf DynamoDB durch authentifizierten cognito-Benutzer (Benutzerpool) in swift
Ich habe folgendes:
- Benutzer-Pools auf AWS Cognito erstellen.
- Erstellen Sie einen Identitätspool in AWS Cognito, und legen Sie die Benutzerpool-ID und App-Client-ID für Cognito auf dem Authentifizierungsanbieter fest.
- Erstellen Sie SampleTable auf AWS DynamoDB.
- Berechtigungsrolle für Berechtigung festlegen, um auf SampleTable in AWS IAM zuzugreifen.
habe ich diesen Code:
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
})
Aber ich habe diesen Fehler, wenn "dynamoDBObjectMapper.scan()" ausgeführt wurde:
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"}
Vielleicht fehlt mir wie Authentication Token zu verwenden, . Was ist falsch an meinem Code?
danke.
Vielen Dank für Ihre Antwort. Die Initialisierungsmethode von AWSStaticCredentialsProvider hat keinen Parameter sessionKey. Also lösche ich den Code "AWSStaticCredentialsProvider initialisieren" und "defaultServiceConfiguration es registrieren" Aber ich bekam Same Error. – hiro