Home > front end >  Why Sign In With Google using Firebase not work with me?
Why Sign In With Google using Firebase not work with me?

Time:08-11

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

CodePudding user response:

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()); //            
  • Related