Facebook身份验证失败

时间:2018-09-12 00:31:07

标签: java android firebase fragment facebook-authentication

我不知道自己的代码有什么问题,它不会给我任何错误消息。

这是以下代码:SignInFragment

public class SignInFragment extends Fragment implements View.OnClickListener {

private static final String TAG = "email";

private TextView mEmail, mPassword, mHeading, mToRegister, mFckyou;
private EditText eEmail, ePassword;
private Button btnSignin;
private ProgressBar mProgressBar;

//Firebase
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

//Facebook
private CallbackManager callbackManager;
private LoginButton loginButton;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.signin_fragment, container, false);

    //TextView
    mEmail = view.findViewById(R.id.tvEmail);
    mPassword = view.findViewById(R.id.tvPassword);
    mHeading = view.findViewById(R.id.heading);
    mToRegister = view.findViewById(R.id.toRegister);
    mFckyou = view.findViewById(R.id.fckyou);
    //EditText
    eEmail = view.findViewById(R.id.etEmail);
    ePassword = view.findViewById(R.id.etPassword);
    //Changing the font style of hint
    eEmail.setTypeface(Typeface.SANS_SERIF);
    ePassword.setTypeface(Typeface.SANS_SERIF);
    //Button
    btnSignin = view.findViewById(R.id.btnSignin);
    //ProgressBar
    mProgressBar = view.findViewById(R.id.progressBar);
    //Facebook
    loginButton = view.findViewById(R.id.login_button);
    //Firebase
    mAuth = FirebaseAuth.getInstance();

    //Progress Bar and Text view = GONE
    mProgressBar.setVisibility(View.GONE);
    mFckyou.setVisibility(View.GONE);

    setupFbAuth();
    navigateToReigster();
    setupFirebaseAuth();
    signInUser();

    return view;
}

/*
---------------------------------------Facebook-------------------------------------------
 */

public void setupFbAuth(){

    callbackManager = CallbackManager.Factory.create();
    loginButton.setReadPermissions(Arrays.asList("email"));
    loginButton.setFragment(this);
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

            handleFacebookAccessToken(loginResult.getAccessToken());

        }

        @Override
        public void onCancel() {

            Toast.makeText(getActivity(), "Cancelling...", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onError(FacebookException error) {

            Log.d(TAG, "onError: error" + error.getMessage());

        }
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    callbackManager.onActivityResult(requestCode,resultCode,data);
    super.onActivityResult(requestCode, resultCode, data);
//        Fragment fragment = getFragmentManager().findFragmentById(R.id.container);
//        fragment.onActivityResult(requestCode, resultCode, data);
}

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

    AuthCredential credential = FacebookAuthProvider.getCredential(accessToken.getToken());
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    if (task.isSuccessful()){
                        Toast.makeText(getActivity(), "Signed in success!", Toast.LENGTH_SHORT).show();
                        FirebaseUser user = mAuth.getCurrentUser();
                        updateUI(user);
                    }else {

                        Log.d(TAG, "onComplete: authentication_failed");

                        Toast.makeText(getActivity(), "Authentication Failed", Toast.LENGTH_SHORT).show();
                    }

                }
            });
}

private void updateUI(FirebaseUser user) {

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.container, new HomeActivity());
    ft.commit();
}

Logcat:

09-11 17:43:23.889 1790-1806/com.example.asus.loginallfragments D/FA: 
Logging event (FE): user_engagement(_e), 
Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=30178, 
firebase_screen_class(_sc)=FacebookActivity, 
firebase_screen_id(_si)=-2880777475469151614}]
09-11 17:43:23.917 1790-1806/com.example.asus.loginallfragments V/FA: 
Connection attempt already in progress
09-11 17:43:24.001 1790-1790/com.example.asus.loginallfragments D/email: 
handleFacebookAccessToken: {AccessToken token:ACCESS_TOKEN_REMOVED 
permissions:[email, public_profile]}
09-11 17:43:24.017 1790-1790/com.example.asus.loginallfragments 
W/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: 
com.google.firebase.auth.api.internal.zzal@52811010
09-11 17:43:24.045 1790-1806/com.example.asus.loginallfragments D/FA: 
Logging event (FE): screen_view(_vs), 
Bundle[{firebase_event_origin(_o)=auto, 
firebase_previous_class(_pc)=FacebookActivity, 
firebase_previous_id(_pi)=-2880777475469151614, 
firebase_screen_class(_sc)=MainActivity, 
firebase_screen_id(_si)=-2880777475469151615}]
09-11 17:43:24.085 1790-1806/com.example.asus.loginallfragments V/FA: 
Connection attempt already in progress
09-11 17:43:24.097 1790-1821/com.example.asus.loginallfragments D/dalvikvm: 
GC_FOR_ALLOC freed 603K, 15% free 4150K/4828K, paused 7ms, total 8ms
09-11 17:43:24.097 1790-1806/com.example.asus.loginallfragments V/FA: 
Connection attempt already in progress
Activity resumed, time: 269011
09-11 17:43:24.101 1790-1806/com.example.asus.loginallfragments D/FA: 
Connected to remote service
09-11 17:43:24.105 1790-1806/com.example.asus.loginallfragments V/FA: 
Processing queued up service tasks: 4
09-11 17:43:26.265 1790-1790/com.example.asus.loginallfragments D/email: 
onComplete: authentication_failed

1 个答案:

答案 0 :(得分:0)

如下更改 readPermissions

使用:

loginButton.setReadPermissions("email", "public_profile");

而不是:

loginButton.setReadPermissions(Arrays.asList("email"));

它为您工作。祝您好运!