skip to Main Content

I am trying to use Google Sihn In with Firebase but it is not working. I have been trying for hours on this please help.

This is my Code:


public class LoginActivity extends AppCompatActivity {

    

    private RelativeLayout signInWithGoogleBtn;

    private GoogleSignInOptions gso;
    private GoogleSignInClient mGoogleSignInClient;

    private static final int RC_SIGN_IN = 123;


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

        signInWithGoogleBtn = findViewById(R.id.signInWithGoogleBtn);

        mAuth = FirebaseAuth.getInstance();

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

        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);


        signInWithGoogleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                googleSignIn();
            }
        });
    }



    private void googleSignIn() {
        Intent intent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(intent, RC_SIGN_IN);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            try {
                GoogleSignInAccount account = task.getResult(ApiException.class);
                firebaseAuthWithGoogle(account.getIdToken());
            } catch (ApiException e) {
                Toast.makeText(LoginActivity.this, "Error", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void firebaseAuthWithGoogle(String idToken) {

        AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {

                            Toast.makeText(LoginActivity.this, "Google Sign In Success", Toast.LENGTH_SHORT).show();

                            FirebaseUser user = mAuth.getCurrentUser();

                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                            LoginActivity.this.finish();

                        } else {
                            Toast.makeText(LoginActivity.this, "Failed to Sign In with Google! Please check your credentials.", Toast.LENGTH_LONG).show();

                        }
                    }
                });
    }
}

What happens is that when I press the "Sign in with Google" button and choose the account, the "error message" in Toast appears.

Please help and I will be really thankful

2

Answers


  1. Chosen as BEST ANSWER

    Well, I solved my problem.. It was that I wasn't putting the application's SHA-1 into the Firebase project. After I installed it, the problem disappeared.


  2. When a Task fails, it contains an exception with details about why it failed. Log that error message, and you can see the cause of the failure and fix that.

    if (task.isSuccessful()) {
        ...
    } else {
        Log.e("SIGNIN", "Failed to Sign In with Google", task.getException()); // 👈
        Toast.makeText(LoginActivity.this, "Failed to Sign In with Google! Please check your credentials.", Toast.LENGTH_LONG).show();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search