java - AWS Cognito GetCredentialsForIdentity

标签 java amazon-web-services amazon-cognito

我正在尝试使用以下博客文章中概述的方法从 Java 向 S3 验证认知身份:

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

我已成功实现开发人员身份验证提供程序,并且可以为我的认知身份检索有效的 OpenId token 。将文件上传/下载到 S3 时,该 token 可在 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 凭证”。此方法被记录为不需要身份验证的公共(public) API。但是,我注意到,如果在系统变量中配置了有效 IAM 凭证的环境中完成调用,则调用会成功。在不是这种情况的环境中它会失败。

我错过了一些简单的事情吗?

最佳答案

您是否尝试过通过以下方式初始化 AmazonCognitoIdentityClient?

新的 AmazonCognitoIdentityClient(新 AnonymousAWSCredentials());

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

关于java - AWS Cognito GetCredentialsForIdentity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44291377/

相关文章:

ruby-on-rails - AWS 中的私钥管理是否有推荐的解决方案?

amazon-web-services - 如何使用 Cognito Id(+配置)调用 AWS API 网关端点?

amazon-web-services - 上次成功/失败登录时间 AWS Cognito

java - 使用批量数据设计独立于数据源的应用程序

java - readObject 方法仅读取它接收到的第一个对象

java - Android Studio 更新到 2.2.2 时无法识别应用程序名称

php - 从 S3 存储桶中删除大量对象会卡住。为什么?

amazon-web-services - Ec2市场Linux实例权限被拒绝(公钥)

amazon-web-services - 认知 : is it possible to host UI on my domain and redirect to my domain too?

Java - GridBagLayout/Insets 将 JLabel 放置在另一个下