【问题标题】:AWS Amplify Cognito Auth error in Android appAndroid 应用程序中的 AWS Amplify Cognito 身份验证错误
【发布时间】:2021-03-25 22:56:37
【问题描述】:

我正在关注 Amplify 文档以向我的 Android 应用程序添加身份验证。我在这条线上收到了AuthException

Amplify.addPlugin(AWSCognitoAuthPlugin())

我确实创建了一个用户池。我需要以某种方式附加它吗?我猜AWSMobileClient 有问题。

这是我的Application 课程:

class AppUtils : Application() {
    override fun onCreate() {
        super.onCreate()
        try {
            Amplify.addPlugin(AWSCognitoAuthPlugin())
            Amplify.configure(applicationContext)
            Log.d(TAG, "Initialized Amplify")
        } catch (error: AmplifyException) {
            Log.e(TAG, "Could not initialize Amplify", error)
        }
    }
}

这是在 logcat 中显示的错误:

AuthException{message=无法实例化 AWSMobileClient,原因=java.lang.RuntimeException:未使用 Cognito Identity 或 Cognito UserPool。必须至少存在一个才能使用 AWSMobileClient。,recoverySuggestion=有关更多详细信息,请参阅附加的异常}

【问题讨论】:

    标签: android authentication amazon-cognito aws-amplify


    【解决方案1】:

    该错误表明在您的配置文件中找不到身份或用户池。

    首先确保您已完成以下步骤:

    • 放大初始化
    • 放大添加授权
    • 放大推送

    完成后,您的app/src/main/res/raw 目录中应该有一个amplifyconfiguration.json 和一个awsconfiguration.json

    您的amplifyconfiguration.json 应如下所示:

    {
        "UserAgent": "aws-amplify-cli/2.0",
        "Version": "1.0",
        "auth": {
            "plugins": {
                "awsCognitoAuthPlugin": {
                    "UserAgent": "aws-amplify-cli/0.1.0",
                    "Version": "0.1.0",
                    "IdentityManager": {
                        "Default": {}
                    },
                    "CredentialsProvider": {
                        "CognitoIdentity": {
                            "Default": {
                                "PoolId": "us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
                                "Region": "us-east-1"
                            }
                        }
                    },
                    "CognitoUserPool": {
                        "Default": {
                            "PoolId": "us-east-1_xxxxxxxxx",
                            "AppClientId": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
                            "AppClientSecret":
    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                            "Region": "us-east-1"
                        }
                    },
                    "Auth": {
                        "Default": {
                            "authenticationFlowType": "USER_SRP_AUTH"
                        }
                    }
                }
            }
        }
    }
    

    您的awsconfiguration.json 应如下所示:

    {
        "UserAgent": "aws-amplify-cli/0.1.0",
        "Version": "0.1.0",
        "IdentityManager": {
            "Default": {}
        },
        "CredentialsProvider": {
            "CognitoIdentity": {
                "Default": {
                    "PoolId": "us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
                    "Region": "us-east-1"
                }
            }
        },
        "CognitoUserPool": {
            "Default": {
                "PoolId": "us-east-1_xxxxxxxxx",
                "AppClientId": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
                "AppClientSecret":
    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                "Region": "us-east-1"
            }
        },
        "Auth": {
            "Default": {
                "authenticationFlowType": "USER_SRP_AUTH"
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果您刚刚添加了 Cognito,请运行

      amplify update api
      

      使用 Cognito 作为身份验证模式。

      然后运行

      amplify push
      

      【讨论】:

        【解决方案3】:

        几天来我也遇到了同样的错误,您刚刚向项目添加了基本文件,现在您需要为 Amplify 定义用户池以向两个配置文件添加更多数据。您需要运行这些命令才能解决此问题。

        amplify init // It will make sure you have basic setup added
        amplify add auth // It will add auth data and user pools to config files
        amplify push // It will push all the setup to amplify cloud
        

        希望它能解决您的问题:)

        【讨论】:

          猜你喜欢
          • 2021-03-17
          • 2021-03-23
          • 2019-08-27
          • 2021-11-24
          • 2018-01-29
          • 1970-01-01
          • 2020-06-01
          • 2021-02-28
          • 2020-01-01
          相关资源
          最近更新 更多