iOS AWS API Gateway:此身份池不支持未经身份验证的访问

时间:2018-10-19 09:11:31

标签: aws-api-gateway amazon-cognito aws-ios

//setup service config
    AWSServiceConfiguration *serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:CognitoIdentityUserPoolRegion credentialsProvider:nil];


    //create a pool


    AWSCognitoIdentityUserPoolConfiguration *configuration = [[AWSCognitoIdentityUserPoolConfiguration alloc] initWithClientId:CognitoIdentityUserPoolAppClientId  clientSecret:nil poolId:CognitoIdentityUserPoolId];

    [AWSCognitoIdentityUserPool registerCognitoIdentityUserPoolWithConfiguration:serviceConfiguration userPoolConfiguration:configuration forKey:@"UserPool"];

    AWSCognitoIdentityUserPool *pool = [AWSCognitoIdentityUserPool CognitoIdentityUserPoolForKey:@"UserPool"];

    pool.delegate = self;

    AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:CognitoIdentityUserPoolRegion
                                                                                                    identityPoolId:CognitoIdentityPoolId];

    AWSServiceConfiguration *configuration1 = [[AWSServiceConfiguration alloc] initWithRegion:CognitoIdentityUserPoolRegion
                                                                          credentialsProvider:credentialsProvider];

    [AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration1;

以上是我在appdelegate中的代码。 我正在使用来自AWS文档“ CognitoYourUserPool”的示例进行登录。 并尝试使用iOS版Generated SDK调用API。

 API_Client *apiInstance = [API_Client defaultClient];
    [apiInstance setAPIKey:@"xxxxxxxxxxxxxxxxxxxxxx"];        
    [[apiInstance presetTypeGet] continueWithBlock:^id _Nullable(AWSTask * _Nonnull t) {
        NSLog(@"error: %@",t.error);
        NSLog(@"result: %@",t.result);
        return nil;
    }];

我确实在登录后调用了API。但这是错误的。 我现在不知道为什么。 请帮我。如果您需要更多信息,请询问。 非常感谢。

2 个答案:

答案 0 :(得分:0)

尝试一下:

AWSCognitoIdentityUserPoolConfiguration *configuration = [[AWSCognitoIdentityUserPoolConfiguration alloc] initWithClientId:CognitoIdentityUserPoolAppClientId  clientSecret:nil poolId:CognitoIdentityUserPoolId];

AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:CognitoIdentityUserPoolRegion identityPoolId:CognitoIdentityPoolId];
AWSServiceConfiguration *configuration1 = [[AWSServiceConfiguration alloc] initWithRegion:CognitoIdentityUserPoolRegion credentialsProvider:credentialsProvider];

[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration1;

[AWSCognitoIdentityUserPool registerCognitoIdentityUserPoolWithConfiguration: configuration1 userPoolConfiguration:configuration forKey:@"UserPool"];

AWSCognitoIdentityUserPool *pool = [AWSCognitoIdentityUserPool CognitoIdentityUserPoolForKey:@"UserPool"];

pool.delegate = self;

答案 1 :(得分:0)

两天后,我知道了。

 AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:CognitoIdentityUserPoolRegion
                                                                                                    identityPoolId:CognitoIdentityPoolId identityProviderManager:pool];

需要设置identityProviderManager。 非常感谢。