简体   繁体   English

AWS Cognito android 未经身份验证的凭证问题

[英]AWS Cognito android unauthenticated credentials issue

I am trying to integrate a Cognito identity pool with an android application and firstly need to be able to access AWS resources from an unauthenticated user.我正在尝试将 Cognito 身份池与 android 应用程序集成,首先需要能够从未经身份验证的用户访问 AWS 资源。 I am using the sample code provided in the Cognito console but when I try to run that section I keep getting errors.我正在使用 Cognito 控制台中提供的示例代码,但是当我尝试运行该部分时,我不断收到错误消息。 I have created a sandbox environment with only one button and all it is doing is running the code provided by the Cognito console.我创建了一个只有一个按钮的沙盒环境,它所做的只是运行 Cognito 控制台提供的代码。

Here is the code:这是代码:

CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
    getApplicationContext(),
    "[IDENTITYPOOLID]", // Identity pool ID
    Regions.US_EAST_1 // Region
);

Here are the errors:以下是错误:

I/AWSKeyValueStore: Detected Android API Level = 30
    Creating the AWSKeyValueStore with key for sharedPreferencesForData = com.amazonaws.android.auth
E/AWSKeyValueStore: com.amazonaws.internal.keyvaluestore.KeyNotFoundException: Error occurred while accessing AndroidKeyStore to retrieve the key for keyAlias: com.amazonaws.android.auth.aesKeyStoreAlias
I/AWSKeyValueStore: Deleting the encryption key identified by the keyAlias: com.amazonaws.android.auth.aesKeyStoreAlias
E/AWSKeyValueStore: Error in retrieving the decryption key used to decrypt the data from the persistent store. Returning null for the requested dataKey = [IDENTITYPOOLID].identityId
D/CognitoCachingCredentia: Loading credentials from SharedPreferences
E/AWSKeyValueStore: com.amazonaws.internal.keyvaluestore.KeyNotFoundException: Error occurred while accessing AndroidKeyStore to retrieve the key for keyAlias: com.amazonaws.android.auth.aesKeyStoreAlias
I/AWSKeyValueStore: Deleting the encryption key identified by the keyAlias: com.amazonaws.android.auth.aesKeyStoreAlias
E/AWSKeyValueStore: Error in retrieving the decryption key used to decrypt the data from the persistent store. Returning null for the requested dataKey = [IDENTITYPOOLID].expirationDate
E/AWSKeyValueStore: com.amazonaws.internal.keyvaluestore.KeyNotFoundException: Error occurred while accessing AndroidKeyStore to retrieve the key for keyAlias: com.amazonaws.android.auth.aesKeyStoreAlias
I/AWSKeyValueStore: Deleting the encryption key identified by the keyAlias: com.amazonaws.android.auth.aesKeyStoreAlias
E/AWSKeyValueStore: Error in retrieving the decryption key used to decrypt the data from the persistent store. Returning null for the requested dataKey = [IDENTITYPOOLID].identityId

All other questions and documentation with the same errors have not provided a solution that works.具有相同错误的所有其他问题和文档均未提供有效的解决方案。

Android studio: 4.0 Android工作室:4.0

Emulators tried:模拟器试过:

Pixel 3 XL API 30
Pixel 2 API 27

Gradle dependencies: Gradle 依赖:

    implementation 'com.amazonaws:aws-android-sdk-cognitoidentityprovider:2.16.12'
    implementation 'com.amazonaws:aws-android-sdk-core:2.16.12'

Identity Pool has access to unauthenticated identities enabled and a role associated with the unauthenticated identities.身份池可以访问启用的未经身份验证的身份以及与未经身份验证的身份关联的角色。 Please advise on how to get the credentialsProvider working for use with other AWS services from an Identity Pool.请告知如何让 credentialsProvider 与身份池中的其他 AWS 服务一起使用。

I encountered same issue我遇到了同样的问题

020-10-03 08:07:14.759 11293-11293/com.example.uandus I/AWSKeyValueStore: Creating the AWSKeyValueStore with key for sharedPreferencesForData = com.amazonaws.android.auth 2020-10-03 08:07:14.772 11293-11293/com.example.uandus E/AWSKeyValueStore: com.amazonaws.internal.keyvaluestore.KeyNotFoundException: Error occurred while accessing AndroidKeyStore to retrieve the key for keyAlias: com.amazonaws.android.auth.aesKeyStoreAlias 020-10-03 08:07:14.759 11293-11293/com.example.uandus I/AWSKeyValueStore: Creating the AWSKeyValueStore with key for sharedPreferencesForData = com.amazonaws.android.auth 2020-10-03 08:07:14.772 11293- 11293/com.example.uandus E/AWSKeyValueStore: com.amazonaws.internal.keyvaluestore.KeyNotFoundException: Error occurred while accessing AndroidKeyStore to retrieve the key for keyAlias: com.amazonaws.android.auth.aesKeyStoreAlias

Following are things I did which worked for me:以下是我所做的对我有用的事情:

  1. Missing details in aws configuration file. aws 配置文件中缺少详细信息。 Make sure you have awsconfiguration.json in app/res/raw and have following content确保您在 app/res/raw 中有 awsconfiguration.json 并具有以下内容

    { "Version": "0.1.0", "IdentityManager": { "Default": {} }, "CredentialsProvider": { "CognitoIdentity": { "Default": { "PoolId": "us-east-1:XXXXX", "Region": "us-east-1" } } }, "CognitoUserPool": { "Default": { "AppClientId": "XXXXX", "PoolId": "us-east-1_XXXXX", "Region": "us-east-1" } } }

Please replace XXX with actual values, you must have these in aws console and search for cognito, you should see Manage user pool and Manage identity pool, if you haven't created please create user pool and pool and then enter identify poolid and user pool poolid and c lientid details in your aws configuration file请将XXX替换为实际值,您必须在aws控制台中有这些并搜索cognito,您应该看到Manage user pool和Manage identity pool,如果您还没有创建请创建用户池和池然后输入识别池ID和用户池您的 aws 配置文件中的 poolid 和 c lientid 详细信息

  1. If may probably get below error如果可能会低于错误

    Process: com.example.uandus, PID: 15529 android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.进程:com.example.uandus,PID:15529 android.os.NetworkOnMainThreadException 在 android.os.StrictMode$AndroidGuardPolicy.

Make sure you below code before hitting dynamodb or any aws service在点击 dynamodb 或任何 aws 服务之前,请确保您低于代码

  int SDK_INT = android.os.Build.VERSION.SDK_INT;
    if (SDK_INT > 8)
    {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

These options worked for me.这些选项对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM