问题
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