AWS Amplify Auth - 获取登录用户的用户属性时出错

时间:2020-12-22 11:44:53

标签: android aws-amplify

我正在尝试在我的 AWS Amplify Auth 应用中使用 Android。注册和登录有效,现在我正在尝试获取已登录用户的用户属性,但 Amplify 在 Amplify.Auth.fetchUserAttributes() 函数中引发异常。

我的登录代码:

private void login(String email, String password) {
        Amplify.Auth.signIn(
                email,
                password,
                result -> {
                    if (result.isSignInComplete()) {
                        Log.i(TAG, "Sign in succeeded");
                        Log.i(TAG, "going to fetch User attributes");
                        
                            Amplify.Auth.fetchUserAttributes(
                                    attributes -> Log.i("AuthDemo", "User attributes = " + attributes.toString()),
                                    error -> Log.e("AuthDemo", "Failed to fetch user attributes.", error)
                            );

                        Log.i(TAG, "end of fetching User attributes");
                    } else {
                        Log.i(TAG,  "Sign in not complete");
                    }

                },
                error -> Log.e(TAG, error.toString())
        );
    }

日志:

I/LoginActivity: Sign in succeeded
W/InternalCallback: Library attempted to call user callback twice, expected only once
I/LoginActivity: going to fetch User attributes
D/AWSMobileClient: Inspecting user state details
D/AWSMobileClient: waitForSignIn: userState:SIGNED_OUT
E/AuthDemo: Failed to fetch user attributes.
    AuthException{message=Failed to fetch user attributes, cause=java.lang.Exception: Operation requires a signed-in state, recoverySuggestion=Ensure that you are logged in and online}
        at com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin$15.onError(AWSCognitoAuthPlugin.java:736)
        ....
     Caused by: java.lang.Exception: Operation requires a signed-in state
        ...

它告诉我用户状态是:SIGNED_OUT,但我在成功登录方法中调用它,那么为什么用户退出了?

1 个答案:

答案 0 :(得分:3)

请在 app -> src -> res -> raw -> amplifyconfiguration.json & awsconfiguration.json 中检查您的身份 poolId 是否正确(即使您没有使用访客访问)。您可以在 AWS 控制台中找到您的 poolId -> cognito -> 管理身份池 -> 单击您的池 -> 单击示例代码:

enter image description here