1

I'm trying to use authentication via google's firebase but facing a problem

In the code below "GoogleSignInResult result"'s value is always false, What should I do?

I've configured the project properly in firebase console, added the JSON file in app directory

@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) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        Log.d("FALSE", String.valueOf(result.isSuccess()));
        if (result.isSuccess()) {
            // Google Sign In was successful, authenticate with Firebase
            GoogleSignInAccount account = result.getSignInAccount();
            firebaseAuthWithGoogle(account);
        } else {
            // Google Sign In failed, update UI appropriately
            // [START_EXCLUDE]
            updateUI(null);
            // [END_EXCLUDE]
        }
    }
}
Mufad
  • 191
  • 1
  • 15

3 Answers3

2

You have check your generated SHA1 key is same as in OAuth 2.0 client ID's Signing-certificate fingerprint. Both keys are must be same

Dhayalu
  • 113
  • 2
  • 12
0

This is a working code, you may try this:

     GoogleApiClient mGoogleApiClient;
        private static final int RC_SIGN_IN = 9001;    

        @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();

            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .enableAutoManage(this /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() {
                        @Override
                        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                            Log.d(TAG, "onConnectionFailed:" + connectionResult);
                        }
                    } /* OnConnectionFailedListener */)
                    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                    .build();

            SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);


            assert signInButton != null;
            signInButton.setSize(SignInButton.SIZE_STANDARD);

            signInButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (v.getId()) {
                        case R.id.sign_in_button:
                            signIn();
                            break;
                    }
                }
            });

    }


        private void handleSignInResult(GoogleSignInResult result) {
            Log.d("Login Google", "handleSignInResult:" + result.isSuccess());
            if (result.isSuccess()) {
                // Signed in successfully, show authenticated UI.
                GoogleSignInAccount acct = result.getSignInAccount();
                firebaseAuthWithGoogle(acct);

                Toast.makeText(this, "User name: " + acct.getDisplayName(), Toast.LENGTH_SHORT).show();

            } else {
                // Signed out, show unauthenticated UI.

            }
        }


        // Firebase with Google
        private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
            Log.d(TAG, "firebaseAuthWithGoogle:" + 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 (!task.isSuccessful()) {
                                Log.w(TAG, "signInWithCredential", task.getException());
                                Toast.makeText(LoginActivity.this, "Authentication failed.",
                                        Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
        }


        private void signIn() {
            Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
            startActivityForResult(signInIntent, RC_SIGN_IN);
        }


        @Override
        protected 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) {
                GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
                handleSignInResult(result);
            }
        }
MorZa
  • 2,215
  • 18
  • 33
  • Log.d("Login Google", "handleSignInResult:" + result.isSuccess()); This is still false, can you tell me what else could be wrong? – Mufad Aug 15 '16 at 12:03
  • Well I got a false result when I changed computers and the SHA1 key has changed... try to go to project settings on the Firebase console and make sure your SHA1 key is correct. – MorZa Aug 15 '16 at 12:22
  • I checked, Tell me one thing, Do I have to generate SHA1 key for different different keystore, or the default one will work for all? – Mufad Aug 15 '16 at 14:08
  • I know there are a debug.keystore and a keystore for production, in developing you should use the debug.keystore. – MorZa Aug 16 '16 at 09:08
0

Step 1: In Firebase project you can add SHA1 debug.keystore or if it’s for production add the SHA1 keystore release

Step 2: In the console google developer add an « id clients OAuth » for debug or for release it depends on step 1

I hope it help you

Raph
  • 41
  • 1