我使用API网关很长一段时间了,我只是略微更改了API以允许使用另一个函数。我进入api网关并测试了该功能以确保它能正常工作。然后我在iPhone上试用了一下,效果也很好。后来,我开始重复出现{errorMessage:"任务在3.00秒后超时"}的实例。我不明白为什么一个简单的登录方法(API网关中的方法)会超时,尤其是因为我在iPhone上测试了输入(之前它工作时),并直接使用API网关控制台测试了输入。
注释:我没有使用生成的sdk或AWSAPIGatewayClient。我只是在做一个http请求。
http登录请求
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *post = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
[defaults objectForKey:@"username"], @"username",
[defaults objectForKey:@"password"], @"password",
nil];
NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject:post options:0 error:&error];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"someLoginEndpoint"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *newJSON = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&error];
if(!newJSON || [newJSON objectForKey:@"errorMessage"]){
NSLog(@"%@",newJSON);
callBack(false);
NSLog(@"DID NOT AUTHENTICATE");
}else{
NSLog(@"%@",newJSON);
[defaults setValue:[newJSON objectForKey:@"Token"] forKey:@"Token"];
[defaults setValue:[newJSON objectForKey:@"IdentityId"] forKey:@"IdentityId"];
[self authenticateUser:^(BOOL call){
callBack(call);
}];
}
}] resume];
刷新方法
- (AWSTask *)refresh {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![self authenticatedWithProvider]) {
return [super getIdentityId];
}else{
NSDictionary *post = [[NSDictionary alloc] initWithObjectsAndKeys:
[defaults objectForKey:@"username"], @"username",
[defaults objectForKey:@"password"], @"password",
nil];
NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject:post options:0 error:&error];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"someLoginEndpoint"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
__block BOOL isLogged = false;
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *newJSON = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&error];
isLogged = true;
if(!newJSON){
NSLog(@"DID NOT AUTHENTICATE");
}else{
NSLog(@"The IdentityID in the refresh method: %@",[newJSON objectForKey:@"IdentityId" ]);
NSLog(@"The token in the refresh method: %@",[newJSON objectForKey:@"Token" ]);
self.identityId = [newJSON objectForKey:@"IdentityId" ];
self.token = [newJSON objectForKey:@"Token" ];
}
}] resume];
return [super getIdentityId];
}
return [super getIdentityId];
}
验证用户
//BusytimeAuthenticated
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
id<AWSCognitoIdentityProvider> identityProvider = [[BusytimeAuthenticated alloc] initWithRegionType:AWSRegionUSEast1
identityId:nil
identityPoolId:@"somePoolID"
logins:@{@"cognito-identity.amazonaws.com": [defaults objectForKey:@"Token"]}
providerName:@"cognito-identity.amazonaws.com"
];
credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1
identityProvider:identityProvider
unauthRoleArn:nil
authRoleArn:nil];
configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1
credentialsProvider:self.credentialsProvider];
AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;
[[credentialsProvider refresh] continueWithBlock:^id(AWSTask *task){
callBack(true);
return nil;
}];
}
错误无法刷新。错误为[Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=10"(null)"UserInfo={message=无效登录令牌。无法传入Cognito令牌。,__type=NotAuthorizedException}]
我的基本问题是,为什么这个操作如此不可靠?它偶尔可以登录我的用户,然后当我使用刷新方法时,我会传递相同的凭据,但并行请求会导致第二个请求失败。
关于超时:
如评论中所述,Lambda冷启动可能会导致您的API网关调用超时,您可能需要优化Lambda以避免超时。
关于刷新错误:
您在登录映射中使用cognitoidentity.amazonaws.com,但使用IdentityProvider模式进行刷新。这就是为什么第一次身份验证成功,但刷新尝试失败的原因。刷新中的逻辑永远不会启动。
我建议您查看我们的端到端示例,了解处理开发人员身份验证的建议流程。
如果您想在登录映射中继续使用cognito identity.amazonaws.com,那么您的令牌刷新实际上需要在身份/凭据提供商的外部进行处理,类似于您处理Facebook令牌的方式。