簡體   English   中英

Android 應用程序中的 AWS Amplify Cognito 身份驗證錯誤

[英]AWS Amplify Cognito Auth error in Android app

我正在關注 Amplify 文檔以向我的 Android 應用程序添加身份驗證。 我在這條線上得到了一個AuthException

Amplify.addPlugin(AWSCognitoAuthPlugin())

我確實創建了一個用戶池。 我需要以某種方式附加它嗎? 我猜AWSMobileClient有一些問題。

這是我的Application class:

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=有關更多詳細信息,請參閱附加的例外}

該錯誤表明在您的配置文件中沒有找到 Identity 或 UserPool。

首先確保您已完成以下步驟:

  • 放大初始化
  • 放大添加身份驗證
  • 放大推動

完成后,您的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"
        }
    }
}

如果您剛剛添加了 Cognito,請運行

amplify update api

使用 Cognito 作為身份驗證模式。

然后運行

amplify push

幾天來我也面臨同樣的錯誤,您剛剛向項目添加了基本文件,現在您需要為 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

希望它能解決你的問題:)

我有同樣的問題。問題是我在終端中配置登錄方法時。

有 3 種受支持的登錄方法:

  1. 登錄(用戶名 - 通過您自己的本機 UI 登錄的密碼)
  2. signInWithWebUi(用戶名 - 通過托管 UI webview 登錄的密碼)
  3. signInWithSocialWebUi -(使用 Google、Facebook 等登錄)

對於 (1),您需要選擇“默認配置”。 完成后,運行放大推送。 此時,您應該期望在您的放大配置中沒有 OAuth object.json。 對於 (2) 或 (3),您需要選擇“使用 Social Provider (Federation) 的默認配置”。 誠然,措辭可能有點令人困惑,因為 (2) 不是社交提供者。 此時,您應該期望在您的放大配置中有一個 OAuth object。json

要更新此用途:

sudo amplify update auth

選擇正確的登錄方式並

sudo amplify push

如果在使用登錄方法2時出現此錯誤: Can't fill in URI in "amplify add auth"

使用這個uri:( https://www.amazon.com/或 http://localhost:3000/)

或者

輸入gettingstarted://

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM