首页 文章

firebase中的Facebook身份验证无法正常工作

提问于
浏览
0

我要做的是使用Firebase登录Facebook,然后获取名称,电子邮件, Profiles 图片和uid,然后将其存储到Firebase数据库 .

一切正常,直到点击登录按钮,然后弹出Facebook帐户窗口 . 之后,当我通过点击“继续使用Rishabh”选择一个帐户时,没有任何反应 .

没有身份验证,没有错误,没有 . 相同的Facebook帐户选择窗口停留在屏幕上,没有任何反应 .

任何帮助将不胜感激 .

这是我的SignInActivity.java:

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

        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");
                }
                // ...
            }
        };

        mSignInToolbar = (Toolbar) findViewById(R.id.signInToolbar);
        setSupportActionBar(mSignInToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mRef = FirebaseDatabase.getInstance().getReference().child("Users");
        mRef.keepSynced(true);

        mEmailField = (EditText) findViewById(R.id.emailField);
        mPasswordField = (EditText) findViewById(R.id.passowrdField);
        mSigninBtn = (Button) findViewById(R.id.signinBtn);
        mProgress = new ProgressDialog(this);

        // Initialize Facebook Login button
        FacebookSdk.sdkInitialize(getApplicationContext());
        mCallbackManager = CallbackManager.Factory.create();
        loginButton = (LoginButton) findViewById(R.id.button_facebook_login);
        loginButton.setReadPermissions("email", "public_profile");
        loginButton.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");
                // ...
            }

            @Override
            public void onError(FacebookException error) {
                Log.d(TAG, "facebook:onError", error);
                // ...
            }
        });

    }


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


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

        // Pass the activity result back to the Facebook SDK
        mCallbackManager.onActivityResult(requestCode, resultCode, data);
    }


    private void handleFacebookAccessToken(AccessToken token) {
        Log.d(TAG, "handleFacebookAccessToken:" + token);
        mProgress.setMessage("Logging in...");
        mProgress.show();

        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(SignInActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                            mProgress.dismiss();
                        }else{
                            String uid=task.getResult().getUser().getUid();
                            String name=task.getResult().getUser().getDisplayName();
                            String email=task.getResult().getUser().getEmail();
                            String image=task.getResult().getUser().getPhotoUrl().toString();

                            DatabaseReference childRef = mRef.child(uid);
                            childRef.child("name").setValue(name);
                            childRef.child("email").setValue(email);
                            childRef.child("image").setValue(image);

                            mProgress.dismiss();

                            Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class);
                            mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(mainIntent);


                        }

                    }
                });
    }

3 回答

  • 0

    看看你需要做的就是在Facebook开发者页面中更改一些权限 . 看下面的图片,确保你也像我一样做了所需的权限"Yes":
    enter image description here

  • 0
    FacebookSdk.sdkInitialize(getApplicationContext());
    

    把它放在应用程序中 .

  • 3

    我通过在真实设备(不是在模拟器中)运行应用程序解决了我的问题 .

相关问题