AWS Api Gateway and Cognito on Android - Error 401

旧巷老猫 提交于 2019-12-08 11:31:49

问题


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();

The result is:

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

回答1:


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.

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.



来源:https://stackoverflow.com/questions/42145091/aws-api-gateway-and-cognito-on-android-error-401

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!