Android上的AWS Api Gateway和Cognito-错误401 [英] AWS Api Gateway and Cognito on Android - Error 401

查看:121
本文介绍了Android上的AWS Api Gateway和Cognito-错误401的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将Cognito与ApiGateway集成时遇到问题。
首先,我使用Cognito登录,在那里没有问题,我得到了所有令牌。
然后,我必须调用API网关。我正在使用自动生成的Android SDK。
我在这里跟随示例: http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-generate-sdk-android.html ,但我总是遇到相同的错误。

I'm having problems integrating Cognito with ApiGateway. First I SignIn with Cognito, not problems there, I get all the tokens. Then I have to make a call to API Gateway. I'm using the auto-generated SDK for Android. I'm following the example here: http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-generate-sdk-android.html but I always get the same error.

CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                    context,
                    CognitoConstants.IDENTITY_POOL_ID,
                    CognitoConstants.REGION);

String token = cognitoUserSession.getIdToken().getJWTToken();

Map<String, String> logins = new HashMap<>();
            logins.put(CognitoConstants.LOGIN_MAP_KEY, token);
            credentialsProvider.setLogins(logins);

ApiClientFactory factory = new ApiClientFactory()
                    .credentialsProvider(credentialsProvider);

client = factory.build(ExampleAPIClient.class);
client.exampleApi1AccountsGet();

结果是:

ApiClientException com.amazonaws.mobileconnectors.apigateway.ApiClientException: {"message":"Unauthorized"}
(Service: BancarAPIClient; Status Code: 401; Error Code: null; Request ID: 3679cd4b-eefc-11e6-9d00-f99040a8ed67)


推荐答案

Android您拥有的代码是从Cognito身份池中获取凭据的,这与Cognito用户池不同。

The Android code you have there is getting credentials from a Cognito Identity Pool, which isn't the same thing as a Cognito User Pool.

我不确定您要使用哪一个,但是如果Android代码正确并且您想通过Twitter / FB / etc登录。 (即身份池),您将需要将API网关方法上的 AuthorizationType 设置为 AWS_IAM 。看起来该API可能是通过Cognito授权器设置的,该授权器用于用户池功能。

I'm not sure which one you want to use, but if the Android code is correct and you want to do signin via Twitter/FB/etc. (which is Identity Pool), you will want to set the AuthorizationType on your API Gateway Methods to AWS_IAM. It looks like the API is probably set up with a Cognito Authorizer which is meant for the User Pools feature.

这篇关于Android上的AWS Api Gateway和Cognito-错误401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆