简体   繁体   中英

Unable to sign-in using Google in Android App

I've tried many solutions mentioned here but I'm still unable to fix this. I'm receiving:

"Google sign in failed. com.google.android.gms.common.api.ApiException: 10"

I've verified my OAuth auto-generated 'web client key' in Google Cloud Console is same as what I'm using in my code. But I'm getting the same error. I've also verified that project name is same in Google Cloud Console and Firebase.

Here's my code:

public class LoginActivity extends Activity {

  private GoogleSignInClient mGoogleSignInClient;
  private FirebaseAuth mAuth = FirebaseAuth.getInstance();
  private static final int RC_SIGN_IN = 1;
  static LoginActivity loginActivity;
  private FirebaseUser currentUser;

  public static LoginActivity getInstance(){
    return loginActivity;
  }
  public FirebaseUser getCurrentUser() {
    return currentUser;
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    GoogleSignInOptions gso = new 
    GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();

    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
    mAuth = FirebaseAuth.getInstance();

    View signInButton = findViewById(R.id.sign_in_button);
    signInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            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) {
        //Here resultCode is 0 & requestCode is 1.  
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            // This line of code is throwing exception.
            GoogleSignInAccount account = task.getResult(ApiException.class);
            Log.w(TAG, task.getException());
            Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId());
            firebaseAuthWithGoogle(account.getIdToken());
        } catch (ApiException e) {
            // Google Sign In failed, update UI appropriately
            Log.w(TAG, "Google sign in failed", e);
        }
    }
}

I'm following this tutorial from official docs. Any help is appreciated.

PS I'm also using Firestore database in the same project. And I've successfully saved the google-services.json in the app's folder.

After Uploading to play store , Play store generate its own SHA1 which we have to replace in second key in google console and Make sure you add your SHA1 to the firebase project.

Go to google console>app signing> copy SH1 generate by console. Add that in google sign in console replace it from 2nd key .

在此处输入图像描述

look at this if it help you: Google sign in failed com.google.android.gms.common.api.ApiException: 10:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM