AWS Cognito GetCredentialsForIdentity

Viewed 3515

I am trying to use the approach outlined in the following blog article to authenticate a cognito identity to S3 from Java:

https://aws.amazon.com/blogs/mobile/understanding-amazon-cognito-authentication-part-4-enhanced-flow/

I have successfully implemented the developer authentication provider and can retrieve a valid OpenId token for my cognito identity. That token works in both the iOS and Android SDKs when uploading/downloading files to S3. Unfortunately, I have not been able to successfully authenticate the same identity to S3 in my Java app. Here is the code I am using:

Map<String, String> logins = new HashMap();
logins.put("cognito-identity.amazonaws.com", cognitoOpenIdToken);
GetCredentialsForIdentityRequest getCredentialsRequest = 
  new GetCredentialsForIdentityRequest()
  .withIdentityId(cognitoIdentityId)
  .withLogins(logins);
AmazonCognitoIdentityClient cognitoIdentityClient = 
  new AmazonCognitoIdentityClient();
GetCredentialsForIdentityResult getCredentialsResult = 
  cognitoIdentityClient.getCredentialsForIdentity(getCredentialsRequest);

The call to getCredentialsForIdentity fails with the error "com.amazonaws.AmazonClientException: Unable to load AWS credentials from any provider in the chain". This method is documented as a public API that does not require authentication. However, I have noticed that the call succeeds if done from an environment where there are valid IAM credentials configured in the system variables. It fails in environments where that is not the case.

Am I missing something simple?

2 Answers
Related