[英]Issues with Amazon Cognito iOS SDK V2 using Facebook and Google+ Provider Authentication
我目前无法使用Facebook和Google+作为提供商授权使用AWS iOS SDK V2的用户。
我不确定它是我在AWS Developer Console上的设置,还是它的代码。
这是标识池的角色策略:
{
"Version": "2012-10-17",
"Statement": [{
"Action": [
"mobileanalytics:PutEvents",
"cognito-sync:*"
],
"Effect": "Allow",
"Resource": ["*"]
}]
我确实收到了未经授权的Cognito ID,但当我尝试使用Facebook或Google+提供商身份验证时,它无效。
一旦Facebook登录返回,我就可以成功使用用户属性来提取个人资料图片,姓名和电子邮件地址。 然后我从Facebook会话中获取令牌(是的,它是一个很长的字符串)并在deviceID类中使用它:
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user {
//Populate viewcontoller with Facebook data
self.profilePictureView.profileID = user.id;
NSRange range = [user.name rangeOfString:@" "];
self.firstName.text = [user.name substringToIndex:range.location];
self.lastName.text = [user.name substringFromIndex:range.location+1];
self.emailAddress.text = [user objectForKey:@"email"];
//Get Facebook token, set then get Cognito device ID - in DeviceId class
NSString *token = FBSession.activeSession.accessTokenData.accessToken;
DeviceId *myDeviceId = [DeviceId sharedInstance];
cognitoDeviceId = [myDeviceId setFacebookToken:token];
}
DeviceID类实现如下所示:
#import "DeviceId.h"
#import <AWSiOSSDKv2/AWSCore.h>
#import <AWSCognitoSync/Cognito.h>
@implementation DeviceId
static NSString *cognitoId;
static DeviceId *_sharedInstance;
static AWSCognitoCredentialsProvider *credentialsProvider;
static AWSServiceConfiguration *configuration;
+ (DeviceId *) sharedInstance
{
if (!_sharedInstance)
{
_sharedInstance = [[DeviceId alloc] init];
}
return _sharedInstance;
}
- (NSString *) getDeviceId
{
return cognitoId;
}
- (void) setDeviceId
{
/*
* AWS Cognito
*/
credentialsProvider = [AWSCognitoCredentialsProvider
credentialsWithRegionType:AWSRegionUSEast1
accountId:@"(accountID"
identityPoolId:@"(identityPool)"
unauthRoleArn:@"arn:aws:iam::(accountID):role/Cognito_(app)UsersUnauth_DefaultRole"
authRoleArn:@"arn:aws:iam::(accountID):role/Cognito_(app)UsersAuth_DefaultRole"];
configuration = [AWSServiceConfiguration configurationWithRegion:AWSRegionUSEast1
credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;
// Retrieve the cognito ID.
cognitoId = credentialsProvider.identityId;
if (!cognitoId) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Identification Error"
message:@"Error on User Account."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
-(NSString *)setFacebookToken:(NSString*)token {
credentialsProvider.logins = @{ @(AWSCognitoLoginProviderKeyFacebook): token };
[self setDeviceId];
return cognitoId;
}
-(NSString *)setGooglePlusToken:(NSString*)token {
credentialsProvider.logins = @{ @(AWSCognitoLoginProviderKeyGoogle): token };
[self setDeviceId];
return cognitoId;
}
@end
我没有收到任何错误消息,上面的仪表板从未显示经过身份验证的用户。 CognitoID永远不会改变它的价值。 谁能告诉我问题出在哪里?
编辑:根据评论更新的DeviceId.m仍然为cognitoId返回nil
编辑2:更新了DeviceId.m以替换while循环检查BFTask是否已完成Bolts方法waitUntilFinished。
#import "DeviceId.h"
#import <AWSiOSSDKv2/AWSCore.h>
#import <AWSCognitoSync/Cognito.h>
@implementation DeviceId
{
__block NSString *tempCognitoId;
}
static NSString *cognitoId;
static DeviceId *_sharedInstance;
static AWSCognitoCredentialsProvider *credentialsProvider;
static AWSServiceConfiguration *configuration;
+ (DeviceId *) sharedInstance
{
if (!_sharedInstance)
{
_sharedInstance = [[DeviceId alloc] init];
}
return _sharedInstance;
}
- (NSString *) getDeviceId
{
return cognitoId;
}
- (void) setDeviceId
{
/*
* AWS Cognito
*/
credentialsProvider = [AWSCognitoCredentialsProvider
credentialsWithRegionType:AWSRegionUSEast1
identityPoolId:@"Identity Pool ID"];
configuration = [AWSServiceConfiguration
configurationWithRegion:AWSRegionUSEast1
credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;
BFTask *taskIdentity = [[credentialsProvider refresh] continueWithBlock:^id(BFTask *task){
if (task.error == nil)
{
tempCognitoId = credentialsProvider.identityId;
NSLog(@"cognitoId: %@", cognitoId);
}
else
{
NSLog(@"Error : %@", task.error);
}
return nil;
}];
[taskIdentity waitUntilFinished];
cognitoId = tempCognitoId;
}
-(NSString *)setFacebookToken:(NSString*)token {
credentialsProvider.logins = @{ @(AWSCognitoLoginProviderKeyFacebook): token };
BFTask *taskIdentity = [[credentialsProvider refresh] continueWithBlock:^id(BFTask *task){
if (task.error == nil)
{
tempCognitoId = credentialsProvider.identityId;
NSLog(@"cognitoId: %@", tempCognitoId);
}
else
{
NSLog(@"Error : %@", task.error);
}
return nil;
}];
[taskIdentity waitUntilFinished];
cognitoId = tempCognitoId;
return cognitoId;
}
-(NSString *)setGooglePlusToken:(NSString*)token {
credentialsProvider.logins = @{ @(AWSCognitoLoginProviderKeyGoogle): token };
BFTask *taskIdentity = [[credentialsProvider getIdentityId] continueWithBlock:^id(BFTask *task){
if (task.error == nil)
{
tempCognitoId = credentialsProvider.identityId;
NSLog(@"cognitoId: %@", tempCognitoId);
}
else
{
NSLog(@"Error : %@", task.error);
}
return nil;
}];
[taskIdentity waitUntilFinished];
cognitoId = tempCognitoId;
return cognitoId;
}
@end
我确实意识到有些人可能会考虑等待完成是不好的做法,但我需要确保测试目的,即“同步”返回cognitoId。 修改Facebook App ID后,使用此方法返回cognitoID。
编辑3:但是,Google+在到达deviceId之前失败了。
- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
error:(NSError *)error {
if (error) {
_signInAuthStatus.text =
[NSString stringWithFormat:@"Status: Authentication error: %@", error];
NSLog(@"%@", error);
return;
}
NSString *idToken = [auth.parameters objectForKey:@"id_token"];
DeviceId *myDeviceId = [DeviceId sharedInstance];
[myDeviceId setGooglePlusToken:idToken];
}
打印出NSLog错误:Error Domain = com.google.GooglePlusPlatform Code = -1“无法完成操作。(com.google.HTTPStatus错误400.)”API和Auth产品名称和当前电子邮件地址似乎是在控制台上更正。
编辑4:代码的另一部分中的Cognito Synch现在已停止工作:
AWSCognito *syncClient = [AWSCognito defaultCognito];
AWSCognitoDataset *dataset = [syncClient openOrCreateDataset:@"myDataSet"];
NSString *fullName = [dataset stringForKey:@"name"];
第一行失败,出现错误:
***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'+ [AWSEndpoint endpointWithRegion:service:]:无法识别的选择器发送给类
对这些额外错误的任何帮助表示赞赏。
看起来,在credentialsProvider.logins映射中设置令牌后,您将通过再次调用setDeviceId来覆盖您的credentialsProvider。 setDeviceId创建一个全新的AWSCognitoCredentialsProvider,它不会将您添加到上一个的令牌,并覆盖变量credentialsProvider。
您描述的问题的常见原因
错误域= com.google.GooglePlusPlatform代码= -1“无法完成操作。(com.google.HTTPStatus错误400.)
在此stackoverflow问题中描述。 我会仔细检查您是否输入了产品名称和电子邮件地址。 此外,我相信您在输入此数据后必须重新创建OAuth凭据,因此如果您还没有这样做,则可能值得尝试。
最后,检查您是否使用Web服务器API密钥, 而不是 iOS客户端密钥。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.