Firebase Facebook身份验证后,mAuth为空

时间:2016-12-22 23:12:20

标签: android firebase firebase-authentication

我一直在努力解决这个问题,我已经尝试了许多来自其他问题的解决方案。基本上,我需要知道我做错了什么才能使onComplete结果为false,使mAuth对象为空。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_login);

    email = (EditText) findViewById(R.id.txtEmail);
    password = (EditText) findViewById(R.id.txtPassword);

    mAuth = FirebaseAuth.getInstance();
    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
        }
    };

    googleSignInBtn = (SignInButton) findViewById(R.id.google_login);
    googleSignInBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            authGoogle();
        }
    });

    mCallbackManager = CallbackManager.Factory.create();
    facebookLoginBtn = (LoginButton) findViewById(R.id.facebook_login_button);
    facebookLoginBtn.setReadPermissions("email", "public_profile");
    facebookLoginBtn.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

            Log.d(TAG, "facebook:onSuccess: " + loginResult);
            handleFacebookAccessToken(loginResult.getAccessToken());
        }

        @Override
        public void onCancel() {

            Log.d(TAG, "facebook:onCancel");
            Toast.makeText(LoginActivity.this, "Facebook login cancelled", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(FacebookException error) {

            Log.d(TAG, "facebook:onError", error);
            Toast.makeText(LoginActivity.this, "Facebook login error", Toast.LENGTH_SHORT).show();
        }
    });

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

    mUserReference = mRootReference.child("User");


}


    private void handleFacebookAccessToken(AccessToken token) {
    Log.d(TAG, "handleFacebookAccessToken:" + token);

    AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
                    Toast.makeText(LoginActivity.this, "Welcome "+mAuth.getCurrentUser().getDisplayName(), Toast.LENGTH_SHORT).show();
                    Intent i = new Intent(LoginActivity.this, MainActivity.class);
                    writeNewAuthenticatedUser(mAuth.getCurrentUser());
                    startActivity(i);
                    signOut();
                    // If sign in fails, display a message to the user. If sign in succeeds
                    // the auth state listener will be notified and logic to handle the
                    // signed in user can be handled in the listener.
                    if (!task.isSuccessful()) {
                        Log.w(TAG, "signInWithCredential", task.getException());
                        Toast.makeText(LoginActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });
}

日志:

    D/Login: onAuthStateChanged:signed_out
    D/Login: facebook:onSuccess: com.facebook.login.LoginResult@1faf11e
    D/Login: handleFacebookAccessToken:{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[public_profile, contact_email, email]}

错误:

    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getDisplayName()' on a null object reference

1 个答案:

答案 0 :(得分:1)

mAuth不为空。 mAuth.getCurrentUser()返回null。这可能意味着signInWithCredential返回的任务未成功完成。如果您要使用addOnCompleteListener,则应检查任务以确保其成功完成。然后才能访问您期望可用的数据(当前用户)。