Android Studio: CognitoCachingCredentialsProvider не работает с исключением KeyNotFoundException - PullRequest
0 голосов
/ 29 мая 2020

Я создаю очень простое приложение, которое одним нажатием кнопки получает учетные данные от AWS Cognito, а затем получает доступ к таблице DynamoDB. Но моя программа терпит неудачу, поскольку CognitoCachingCredentialsProvider не получает никаких учетных данных. Я создал AWS Identity Pool, а также назначил ему роль.

Воспроизвести

CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(), // Context
COGNITO_IDENTITY_POOL_ID, // Identity Pool ID
COGNITO_IDENTITY_POOL_REGION );// Region

Этот код не работает с

E/AWSKeyValueStore:    com.amazonaws.internal.keyvaluestore.KeyNotFoundException: 
Error occurred while accessing AndroidKeyStore to retrieve the key for keyAlias:
com.amazonaws.android.auth.aesKeyStoreAlias Deleting the encryption key identified by the keyAlias: 
com.amazonaws.android.auth.aesKeyStoreAlias Error in retrieving the decryption key used to decrypt the data from the persistent store. 
Returning null for the requested dataKey = eu-central-1:ABCDEFGHIJ.identityId

MainActivity. java

package demo.example.studentappdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.mobileconnectors.dynamodbv2.document.Table;
import com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Document;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

  String message = "Default";
private final String COGNITO_IDENTITY_POOL_ID = "eu-central-1:ABCDEFGHIJ" ; ---- As per AWS
private final Regions COGNITO_IDENTITY_POOL_REGION = Regions.EU_CENTRAL_1 ;
private final String DYNAMODB_TABLE = "XX_STUDENT_TABLE" ;
private Context context ;
private CognitoCachingCredentialsProvider credentialsProvider ;
private AmazonDynamoDBClient dbClient ;
private Table dbTable ;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
/** Called when the user touches the button */
public void findStudent(View view)
{
    // Do something in response to button click
     EditText editText = (EditText) findViewById(R.id.StudNameTxt);

         CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                 getApplicationContext(), // Context
                 COGNITO_IDENTITY_POOL_ID, // Identity Pool ID
                 COGNITO_IDENTITY_POOL_REGION );// Region

         AmazonDynamoDBClient dbClient = new AmazonDynamoDBClient(credentialsProvider);
         dbClient.setRegion(Region.getRegion(Regions.EU_CENTRAL_1));
         dbTable = Table.loadTable(dbClient, DYNAMODB_TABLE);

}

build.gradle:

dependencies {
  implementation fileTree(dir: 'libs', include: ['*.jar'])

  implementation 'androidx.appcompat:appcompat:1.1.0'
  implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  testImplementation 'junit:junit:4.12'
  androidTestImplementation 'androidx.test.ext:junit:1.1.1'
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
  implementation 'com.amazonaws:aws-android-sdk-ddb-mapper:2.16.12'
  implementation 'com.amazonaws:aws-android-sdk-core:2.16.12'
  implementation 'com.amazonaws:aws-android-sdk-ddb-document:2.16.12'
  implementation 'com.amazonaws:aws-android-sdk-mobile-client:2.16.12'

// Также для размещенного пользовательского интерфейса: реализация 'com.amazon aws: aws - android -sdk-auth -userpools: 2.16.12 'реализация' com.amazon aws: aws - android -sdk-auth-ui: 2.16.12 '

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

AndroidManifest. java:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="demo.example.studentappdemo">
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Android Версия = 3.6.3

Любая помощь будет высоко оценена, так как я полностью застрял

...