2017-05-31 203 views
1

我尝试使用下面的博客文章中介绍的方法来验证从Java cognito身份S3:AWS Cognito GetCredentialsForIdentity

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

我已经成功地实施了开发商的认证供应商,可以检索有效的OpenId令牌用于我的认证身份。当将文件上传/下载到S3时,该令牌在iOS和Android SDK中都可以使用。不幸的是,我无法在我的Java应用程序中成功地向S3验证相同的身份。这里是我使用的代码:

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

到getCredentialsForIdentity调用失败,出现错误“com.amazonaws.AmazonClientException:无法从任何供应商加载链 AWS凭据”。此方法记录为不需要身份验证的公共API。但是,我注意到,如果在系统变量中配置了有效IAM凭证的环境中完成调用,则该调用会成功。如果情况并非如此,它会失败。

我是否缺少一些简单的东西?

回答

1

有你尝试以下列方式初始化AmazonCognitoIdentityClient?

新的AmazonCognitoIdentityClient(new AnonymousAWSCredentials());

否则,通过设计AWS Java SDK,服务客户端将查找AWS凭证,因为同样的客户端也可用于访问经过身份验证的API。

+0

就是这样,谢谢你Vinay! – Mike

0

也许不是你需要另一个IAM用户......但是运行你方法有一个上sts:*“拒绝”用户(或者你需要一些其他操作)

+0

这是在Lambda上运行吗? – UXDart

+0

它不在Lambda上运行。我的理解是,调用这个方法根本不需要IAM用户,它是一个公共方法(不需要凭证来调用它)。 – Mike

+0

同意,但您是否使用其他凭据/ IAM用户可能有“拒绝”的STS? – UXDart