【发布时间】:2017-06-04 08:46:44
【问题描述】:
使用 Firebase Facebook 进行身份验证不起作用,这是我的片段代码:
public class LoginFragment extends Fragment {
private View mView;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private CallbackManager mCallbackManager;
public LoginFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mView = inflater.inflate(R.layout.fragment_login, container, false);
loginFacebook();
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");
}
// ...
}
};
return mView;
}
private void loginFacebook() {
// Initialize Facebook Login button
mCallbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) mView.findViewById(R.id.login_button);
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 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);
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
private void handleFacebookAccessToken(AccessToken token) {
//Log.d(TAG, "handleFacebookAccessToken:" + token);
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener((MainActivity)getActivity(), 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((MainActivity)getActivity(), "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
在 Facebook 弹出窗口出现并输入我的用户名和密码后,在调试模式下代码中没有任何内容。
Firebase 用户始终为 null。我究竟做错了什么 ? 登录后记录 Log.d("LOGGER", "onAuthStateChanged:signed_out");
PS:完全相同的代码在插入 MainActivity
时会起作用【问题讨论】:
-
是否触发过 onComplete 监听器?您能否更详细地说明您的尝试以及结果如何?看起来您已经注释掉了相关日志。您如何验证它实际上是在尝试进行身份验证?见how to ask 和creating an mcve。
-
它进入 AuthStateListener,并给我 Log.d("LOGGER", "onAuthStateChanged:signed_out");
-
它最初会做什么,直到你真正登录。那么,你是如何验证 onComplete 曾经被调用过的?
标签: android facebook android-fragments firebase firebase-authentication