我正在关注this Firebase教程,将Facebook整合到我的应用中。一切看起来都不错,除非我有这个例外:
com.google.firebase.auth.FirebaseAuthUserCollisionException:已存在具有相同电子邮件地址但登录凭据不同的帐户。使用与此电子邮件地址关联的提供商登录。
公平对待,考虑到这正是我正在尝试做的事情。首先,我创建了一个代码,将gmail集成到我的应用程序,并使用我的电子邮件帐户登录。然后我尝试使用Facebook登录,并收到错误。
搜索如何解决这个问题,我找到了here
如果已存在具有凭据声明的电子邮件地址的帐户,则抛出FirebaseAuthUserCollisionException。通过调用fetchProvidersForEmail(String)解决此问题,然后要求用户使用其中一个登录。
最后问题是我不确定我是否正确使用fetchProvidersForEmail
方法,因为我的任务没有成功(检查最后一段代码)。
有人可以给予支持吗? 谢谢,
*以下代码目前支持登录gmail和Facebook。我在这里讲的方法是最后一个,handleFacebookAccessToken
。
public class SignInActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
/**
* References
* https://developers.google.com/identity/sign-in/android/sign-in#configure_google_sign-in_and_the_googleapiclient_object
*/
// Here a simple Sign-In button is used to initiate authentication. In this step you will
// implement the logic to Sign-In with Google then use that Google account to authenticate with
// Firebase.
private static final String TAG = "SignInActivity";
private static final int RC_SIGN_IN = 9001;
private GoogleApiClient mGoogleApiClient;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
LoginButton facebookLogin;
CallbackManager cM;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.activity_sign_in);
cM = CallbackManager.Factory.create();
facebookLogin = (LoginButton) findViewById(R.id.sign_in_button_facebook);
if (facebookLogin != null) {
facebookLogin.setReadPermissions("public_profile", "email", "user_friends");
facebookLogin.registerCallback(cM, 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);
// ...
}
});
}
// Configure Google Sign-In to request the user data required by the app. For example, to
// configure Google Sign-In to request users' ID and basic profile information, create a
// GoogleSignInOptions object with the DEFAULT_SIGN_IN parameter (ID and basic profile are
// included in DEFAULT_SIGN_IN). To request users' email addresses as well, create the
// GoogleSignInOptions object with the requestEmail option.
GoogleSignInOptions mGoogleSignInOptions = new GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
// Create a GoogleApiClient object with access to the Google Sign-In API and the options
// specified. Note: To use enableAutoManage, your activity must extend FragmentActivity or
// AppCompatActivity (a subclass of FragmentActivity), both of which are part of the Android
// Support Library. You can use GoogleApiClient in a Fragment; however, the fragment's
// parent activity must be a FragmentActivity. If you can't extend FragmentActivity, you
// must manually manage the GoogleApiClient connection lifecycle.
mGoogleApiClient = new GoogleApiClient
.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, mGoogleSignInOptions)
.build();
// Configure the sign in button.
SignInButton mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
if (mSignInButton != null) {
mSignInButton.setSize(SignInButton.SIZE_STANDARD);
mSignInButton.setOnClickListener(this);
}
// Initialize FirebaseAuth
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");
}
// ...
}
};
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signIn();
break;
}
}
@Override
public void onResume() {
super.onResume();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onPause() {
super.onPause();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
// Handle sign-in button taps by creating a sign-in intent with the getSignInIntent method, and
// starting the intent with startActivityForResult. Starting the intent prompts the user to
// select a Google account to sign in with. If you requested scopes beyond profile, email, and
// openid, the user is also prompted to grant access to the requested resources.
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
// An unresolvable error has occurred and Google APIs (including Sign-In) will not be available.
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed:" + connectionResult);
Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show();
}
// Handle the result of the Google Sign In in the activity's onActivityResult method, which
// retrieve the sign-in result with getSignInResultFromIntent.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
cM.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
// After you retrieve the sign-in result, you can check if sign-in succeeded with the isSuccess
// method. If sign-in succeeded, you can call the getSignInAccount method to get a
// GoogleSignInAccount object that contains information about the signed-in user, such as the
// user's name. You can also get the user's email address with getEmail, the user's Google ID
// (for client-side use) with getId, and an ID token for the user with with getIdToken. If you
// need to pass the currently signed-in user to a backend server, send the ID token to your
// backend server and validate the token on the server.
public void handleSignInResult(GoogleSignInResult result) {
if (result.isSuccess()) {
GoogleSignInAccount account = result.getSignInAccount();
// Google Sign In was successful, authenticate with Firebase.
firebaseAuthWithGoogle(account);
} else {
// Google Sign In failed
// TODO: 26/06/2016 Handle no internet connection
Log.e(TAG, "Google Sign In failed.");
}
}
// Add the required firebaseAuthWithGoogle method to authenticate with the signed in Google
// account.
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGooogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
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();
} else {
startActivity(new Intent(SignInActivity.this, MainActivity.class));
finish();
}
}
});
}
private void handleFacebookAccessToken(AccessToken token) {
Log.d(TAG, "handleFacebookAccessToken:" + token);
// This commented block below is what is on the tutorial, but gives the exceptio explained.
/*
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();
}
// ...
}
});
*/
mAuth.fetchProvidersForEmail("i_put_here_my_email@gmail.com").addOnCompleteListener(this, new OnCompleteListener<ProviderQueryResult>() {
@Override
public void onComplete(@NonNull Task<ProviderQueryResult> 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();
} else {
startActivity(new Intent(SignInActivity.this, MainActivity.class));
finish();
}
}
});
}
}
答案 0 :(得分:2)
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mFirebaseAuth.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();
//((FirebaseAuthException)task.getException()).getErrorCode();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if(e instanceof FirebaseAuthUserCollisionException)
{
//Handle Auth Error Here
Toast.makeText(SignInActivity.this, e.toString(),
Toast.LENGTH_SHORT).show();
}else if(e instanceof FirebaseAuthInvalidUserException)
{
//Handle Auth Error Here
Toast.makeText(SignInActivity.this, e.toString(),
Toast.LENGTH_SHORT).show();
}
}
});
注意:如果我们能够获得那些Auth异常的错误代码,那么这比使用类型检查(instanceOf)更好