Facebook使用Firebase身份验证登录

时间:2016-12-15 02:04:32

标签: android firebase firebase-authentication facebook-login

我在Android应用程序中使用Firebase Auth,但我遇到了问题。

我可以通过手机将我的应用链接到我的Facebook,一旦链接,我的Firebase用户仍然null。我不知道为什么。

有人知道发生了什么吗?以下是我的代码。

我按照Firebase GitHub repo上的步骤操作。我使用的是Firebase 9.0.1。

mAuth = FirebaseAuth.getInstance();

    mCallbackManager = CallbackManager.Factory.create();
    LoginButton loginButton = (LoginButton) findViewById(R.id.facebook_connexion_button);
    loginButton.setReadPermissions("email", "public_profile");
    loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.d(TAG, "facebook:onSuccess:" + loginResult);
            startActivity(new Intent(ConnectActivity.this,Events.class));
            handleFacebookAccessToken(loginResult.getAccessToken());
        }

        @Override
        public void onCancel() {
            Log.d(TAG, "facebook:onCancel");
            // [START_EXCLUDE]
            //updateUI(null);
            // [END_EXCLUDE]
        }

        @Override
        public void onError(FacebookException error) {
            Log.d(TAG, "facebook:onError", error);
            // [START_EXCLUDE]
            //updateUI(null);
            // [END_EXCLUDE]
        }
    });

mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                dataIntoPref(user.getDisplayName(),String.valueOf(user.getPhotoUrl()));
                Toast.makeText(ConnectActivity.this,user.getDisplayName(),Toast.LENGTH_SHORT).show();
                startActivity(new Intent(ConnectActivity.this,Events.class));
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
            } else {
                // User is signed out
                Toast.makeText(ConnectActivity.this,"User is Null",Toast.LENGTH_SHORT).show();
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
            // ...
        }
    };

 @Override
    public void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mAuthListener != null) {
            mAuth.removeAuthStateListener(mAuthListener);
        }
    }

handleFacebookAccessToken就是这样的:

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());

                        // 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(ConnectActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }

                        // [START_EXCLUDE]
                        // hideProgressDialog();
                        // [END_EXCLUDE]
                    }
                });

0 个答案:

没有答案