我已经在Firebase walktrough中使用Facebook帐户进行身份验证。即使我仍然无法连接它们。我可以使用Facebook(在应用程序中)登录,但它永远不会在Firebase中以新用户身份存储。 如果你能发现我的错误,请帮助我。
当我尝试使用Facebook(通过Android应用)在Firebase中进行身份验证时,它不会在handleFacebookAccessToken方法中输入addOnCompleteListener
方法。
Logcat最后显示的是这条消息:
-app: Entered handleFacebookAccessToken, credential:com.google.firebase.auth.FacebookAuthCredential*******
我将在我的登录活动中分享我的代码。
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.example.qeri.qeri.MainActivity.MainActivity;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FacebookAuthProvider;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import java.util.Arrays;
public class Login extends AppCompatActivity {
CallbackManager callbackManager;
private TextView textView;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private static final String TAG = "-app";
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.login_screen);
Log.d(TAG, "Layout created in LoginActivity");
//FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
textView = (TextView)findViewById(R.id.Auth_Key);
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("email", "public_profile"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess " + loginResult);
handleFacebookAccessToken(loginResult.getAccessToken());
}
@Override
public void onCancel() {
// App code
textView.setText("Login Canceled");
}
@Override
public void onError(FacebookException exception) {
// App code
textView.setText("Login Failed");
}
});
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
//Log.d(TAG, firebaseAuth.getCurrentUser().toString());
if (user != null) {
// User is signed in
startActivity(new Intent(Login.this, MainActivity.class));
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
}
};
}
private void handleFacebookAccessToken(AccessToken accessToken){
AuthCredential credential = FacebookAuthProvider.getCredential(accessToken.getToken());
Log.d(TAG, "Entered handleFacebookAccessToken, credential:" + credential);
mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "Entered onComplete");
if (!task.isSuccessful()){
Toast.makeText(Login.this,"Authentication failed.",Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.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);
//}
}
}