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