gpt4 book ai didi

android - Firebase 中的 Facebook 身份验证不起作用

转载 作者:行者123 更新时间:2023-12-02 04:02:39 24 4
gpt4 key购买 nike

我想要做的是使用 Firebase 登录 Facebook,然后获取姓名、电子邮件、个人资料图片和 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);


}

}
});
}

最佳答案

看看您需要做的就是更改 Facebook 开发者页面中的某些权限。请参阅下图,确保您也像我一样将所需的权限设置为"is": enter image description here

关于android - Firebase 中的 Facebook 身份验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41166708/

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