gpt4 book ai didi

Android Firebase Oauth Facebook 无法在 fragment 中工作

转载 作者:行者123 更新时间:2023-11-29 17:05:51 25 4
gpt4 key购买 nike

无法使用 Firebase Facebook 进行身份验证,这是我的 fragment 代码:

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 弹出窗口出现并输入我的用户名和密码后,在 Debug模式下代码中没有任何内容被触发。

Firebase 用户始终为 null。我究竟做错了什么 ?登录后记录 Log.d("LOGGER", "onAuthStateChanged:signed_out");

PS:完全相同的代码在插入 MainActivity

时有效

最佳答案

您可以尝试将 fragment 链接到登录按钮。

loginButton.setFragment(this);

关于Android Firebase Oauth Facebook 无法在 fragment 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41745411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com