我已将SHA1添加到我的firebase项目中,还添加了SHA256。我仍然不断收到以下错误。 我也启用了Google登录方法。 请我没有选择权。
我的build.gradle
implementation 'com.google.firebase:firebase-core:16.0.6'
implementation 'com.google.firebase:firebase-auth:16.1.0'
implementation 'com.google.android.gms:play-services-auth:16.0.1'
错误报告:
W/SigninActivity: Google sign in failed
com.google.android.gms.common.api.ApiException: 12500:
at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(Unknown Source)
at com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(Unknown Source)
at org.aac.detailor.SigninActivity.onActivityResult(SigninActivity.java:83)
at android.app.Activity.dispatchActivityResult(Activity.java:6294)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3829)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3876)
at android.app.ActivityThread.access$1300(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1519)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5631)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
这是我的登录活动,我根据Firebase身份验证中的文档执行了“身份验证”步骤。
我的登录活动:
public class SigninActivity extends AppCompatActivity {
FirebaseAuth auth;
GoogleSignInClient mGoogleSignInClient;
SignInButton googleSignIn;
FirebaseAuth.AuthStateListener authStateListener;
private static final int RC_SIGN_IN = 111;
private static final String TAG = "SigninActivity";
public static String username = "org.aac.trial.username";
public static String email = "org.aac.trial.email";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signin);
auth = FirebaseAuth.getInstance();
auth = FirebaseAuth.getInstance();
authStateListener = firebaseAuth -> {
if (firebaseAuth.getCurrentUser() != null) {
Toast.makeText(SigninActivity.this, "Welcome "+
firebaseAuth.getCurrentUser().getDisplayName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(SigninActivity.this, MainActivity.class);
startActivity(intent);
return;
}
};
auth.addAuthStateListener(authStateListener);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken("891757993158-tle1db5q6ems263o723uejcfcn486ush.apps.googleusercontent.com")
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
googleSignIn = findViewById(R.id.signInGoogle);
googleSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn();
}
});
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w(TAG, "Google sign in failed", e);
Toast.makeText(SigninActivity.this, "Google Sign In Failed \n"+e, Toast.LENGTH_LONG).show();
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
auth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = auth.getCurrentUser();
updateUI(user);
Toast.makeText(SigninActivity.this, "Welcome "+user.getDisplayName(), Toast.LENGTH_SHORT).show();
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(SigninActivity.this, "Authentication Failed.", Toast.LENGTH_LONG).show();
}
// ...
}
});
}
private void updateUI(FirebaseUser user) {
Intent intent = new Intent(SigninActivity.this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString(username, user.getDisplayName());
bundle.putString(email, user.getEmail());
intent.putExtras(bundle);
startActivity(intent);
}
}