失败firebase auth facebook

时间:2017-08-16 01:31:13

标签: android firebase firebase-authentication facebook-login

我正在尝试用firebase上的facebook进行身份验证。我已经设置了facebook SDK,我可以完美地登录facebook。

但我无法获得firebase auth工作。该应用在方法onComplete(Task <AuthResult> task)上有一个TOAST,如果在此过程中出现问题,它将显示出来。我收到此TOAST错误,但无法找到问题所在。我已经在firebase文档中设置了所有内容。

public class LoginActivity extends AppCompatActivity {
    private LoginButton loginButton;
    private CallbackManager callbackManager;

    private FirebaseAuth firebaseAuth;
    private FirebaseAuth.AuthStateListener firebaseAuthListener;

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

        firebaseAuth = FirebaseAuth.getInstance();

        callbackManager  = CallbackManager.Factory.create();
        loginButton = (LoginButton) findViewById(R.id.loginButton);
        loginButton.setReadPermissions("email","public_profile");
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                handleFacebookAccessToken(loginResult.getAccessToken());
            }

            @Override
            public void onCancel() {
                Toast.makeText(getApplicationContext(),"Error al iniciar sesión",Toast.LENGTH_LONG).show();

            }

            @Override
            public void onError(FacebookException error) {
                Toast.makeText(getApplicationContext(),"Error al iniciar sesión",Toast.LENGTH_LONG).show();
            }
        });

        firebaseAuthListener = new FirebaseAuth.AuthStateListener(){

            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    goMainScreen();
                }
            }
        };
    }

    private void handleFacebookAccessToken(AccessToken accessToken) {
        Log.d("","handle"+accessToken.getCurrentAccessToken().getToken());
        AuthCredential credential = FacebookAuthProvider.getCredential(accessToken.getCurrentAccessToken().getToken());
        firebaseAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    Toast.makeText(getApplicationContext(), "hi", Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(getApplicationContext(), "error",  Toast.LENGTH_LONG).show(); // here is the ERROR.

                }
            }
        });

    }


    private void goMainScreen() {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onStart() {
        super.onStart();
        firebaseAuth.addAuthStateListener(firebaseAuthListener);
    }

    @Override
    protected void onStop() {
        super.onStop();
        firebaseAuth.removeAuthStateListener(firebaseAuthListener);
    }

}

我已经在firebase上启用了Facebook身份验证,并且已经复制了id和秘密。此外,我已经在Facebook开发者中复制了Firebase网址。

1 个答案:

答案 0 :(得分:1)

该任务包含可能有助于您排除故障的错误信息。不要在祝酒词中显示"error",而是展示task.getException().toString()或更好:用throw task.getException()抛出异常。