【问题标题】:Unauthenticated access is not supported for this identity pool此身份池不支持未经身份验证的访问
【发布时间】:2017-11-07 09:49:23
【问题描述】:

错误是: com.amazonaws.services.cognitoidentity.model.NotAuthorizedException:此身份池不支持未经身份验证的访问。 (服务:AmazonCognitoIdentity;状态码:400;错误码:NotAuthorizedException;

请帮帮我。 登录过程成功,当我进入 Amazon lex 聊天机器人窗口时,我收到上述错误 此身份池不支持未经身份验证的访问。 这实际上是有道理的,因为我未经检查未经身份验证访问我的bot 所以我知道如果没有身份验证,任何用户都不会被允许与 Lex ChatBot 交互。但是现在我的登录成功了,我仍然有这个错误。

我的问题是我们是否需要维护已登录的用户会话?如果是,我该怎么做。

或者

我如何将登录链接到 Cognito Authenticated Identity。

【问题讨论】:

  • 是的,您不需要维护会话。这是通过在身份池的登录映射中设置一个有效的 id 令牌来完成的
  • @agent420 你可以发布一些代码告诉我如何设置该 ID 以及在哪里设置。我对这个概念很陌生。请帮帮我。

标签: android amazon


【解决方案1】:

当身份池禁用未经身份验证的访问并且在应用程序的登录映射中未找到 id 令牌时,我们会收到此错误。例如,在 Android 应用中,初始化 CognitoCachingCredentialsProvider 后,还需要调用 setLogins() 方法并提供登录映射。

//Relevant imports
import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.regions.Regions;

//Initialize credentials provider
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
  getApplicationContext(),
  "IDENTITY_POOL_ID", 
  Regions.US_EAST_1 
);

//Create a login map
Map<String, String> logins = new HashMap<String, String>();
logins.put("www.amazon.com", "login with Amazon token");

//Set login map
credentialsProvider.setLogins(logins);
credentialsProvider.getCredentials();

//Create clients for AWS services with credentialsProvider as a parameter in the constructor

在上面的示例中,我假设使用了“使用亚马逊登录”。对于不同的提供商,使用适当的令牌更改密钥“www.amazon.com”。要知道密钥,只需在https://jwt.io 解码 id 令牌并查找 iss 声明。没有 https:// 的值将是登录映射的键。

至于将这段代码放在哪里,请检查它是否在任何地方使用CognitoCachingCredentialsProvider 对象,然后向其添加登录映射。要强制刷新凭据,请调用refresh() 方法

【讨论】:

  • 非常感谢。你为我节省了很多时间。
  • 你能再告诉我一件事吗?你为什么使用 CognitoCachingCredentialsProvider 而不仅仅是 CognitoCredentialsProvider
  • CognitoCachingCredentialsProvider 是 CognitoCredentialsProvider 的子类,它添加了诸如在 SharedPreferences 中持久化 Cognito id、缓存会话凭据以减少网络请求等功能。
【解决方案2】:

为 aws amplify 启用 multiauth

最后, 您现在要编辑添加到组的功能吗? (是/否)n

【讨论】:

  • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
猜你喜欢
  • 2017-07-01
  • 2019-02-09
  • 2017-06-14
  • 2017-05-31
  • 2019-03-24
  • 2016-06-24
  • 1970-01-01
  • 2016-10-09
  • 2020-08-25
相关资源
最近更新 更多