0

I want to make Google Sign-in Button for my android mobile app. I am using 4 years ago library for making this. But there is a problem. I click to sign-in button. After, I choose my Gmail account. But unfortunately, Sign in is always failed. I have tried too many library versions. And Solutions at stackoverflow.com Such as: Error implementing GoogleApiClient Builder for Android development

Library that I used to:

 implementation 'com.google.android.gms:play-services-auth:17.0.0'

İmplements:

implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener

Here is my code:

sign_in_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                sign_In();

            }

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

                mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
                        .enableAutoManage(enter_screen.this, enter_screen.this)
                        .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                        .build();
                Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
                startActivityForResult(signInIntent, RC_SIGN_IN);






            }
        });

  @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

    }

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


        if (requestCode == RC_SIGN_IN) {

            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if (result.isSuccess()){
                GoogleSignInAccount account = result.getSignInAccount();
                firebaseAuthGoogle(account);

                Toast.makeText(this, "works", Toast.LENGTH_SHORT).show();
            }else {

                // Here is the problem. It returns me this toast message.

                Toast.makeText(this, "faild", Toast.LENGTH_SHORT).show();


            }

        }

    }

    private void firebaseAuthGoogle(GoogleSignInAccount account) {
        AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(),null);
        firebaseAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (!task.isSuccessful()){
                            Toast.makeText(enter_screen.this, "Giriş başarısız", Toast.LENGTH_SHORT).show();

                        }else {
                            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                            startActivity(intent);
                            enter_screen.this.finish();

                        }



                    }
                });


    }


    @Override
    public void onConnected(@Nullable Bundle bundle) {

    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onPause() {
        super.onPause();
        mGoogleApiClient.stopAutoManage(enter_screen.this);
        mGoogleApiClient.disconnect();
    }

My console shows me when I try to sign in:

D/AutoManageHelper: starting AutoManage for client 0 true null
    connecting com.google.android.gms.common.api.internal.zabe@ab0a32d

This words.

How can I fix it? I want to log in and record my Gmail address to Firebase authentication.

  • Since you're using Java, I think that this [resource](https://medium.com/firebase-tips-tricks/how-to-create-a-clean-firebase-authentication-using-mvvm-37f9b8eb7336) will help. Here is the corresponding [repo](https://github.com/alexmamo/FirebaseAuthentication). – Alex Mamo Mar 21 '23 at 06:59
  • No. :( It does not works. – Berkay Isıkoglu Mar 21 '23 at 14:46
  • "It does not work" doesn't provide enough information so we can help. What exactly doesn't work? Do you have any errors? – Alex Mamo Mar 21 '23 at 14:56
  • I do not know how to use your clean code. There is too much unknown knowledge for me. I have no experience with this topic. So, I can not compile your code. Sorry. :/ Can you share more basic code peace? I just want to sign in with Google Sign in. Lastly, I am not using MVVM architecture. – Berkay Isıkoglu Mar 21 '23 at 21:53
  • I downloaded the project. And I am not still add my gmail accounts. Here is the problem: D/FirebaseAuthAppTag: 10: V/FA: Activity resumed, time: 446637 D/FA: Logging event (FE): screen_view(_vs), Bundle[{ga_event_origin(_o)=auto, ga_previous_class(_pc)=SignInHubActivity, ga_previous_id(_pi)=-5122443450998235497, ga_screen_class(_sc)=AuthActivity, ga_screen_id(_si)=-5122443450998235499}] – Berkay Isıkoglu Mar 26 '23 at 22:24
  • Lastly, I found the solution. :) – Berkay Isıkoglu Apr 08 '23 at 07:07

1 Answers1

-2

Answer is here. It can not work because google updated this section.

First of all, add all dependencies to your project:

  implementation platform('com.google.firebase:firebase-bom:31.4.0')

    // Add the dependency for the Firebase Authentication library
    // When using the BoM, you don't specify versions in Firebase library dependencies
    implementation 'com.google.firebase:firebase-auth'

    // Also add the dependency for the Google Play services library and specify its version
    implementation 'com.google.android.gms:play-services-auth:20.4.1'

Global variables:

  private static final String TAG = "GoogleActivity";
    private static final int RC_SIGN_IN = 9001;

    // [START declare_auth]
    private FirebaseAuth mAuth;
    // [END declare_auth]

    private GoogleSignInClient mGoogleSignInClient;

mAuth = FirebaseAuth.getInstance();

Add these codes to your activity:

 GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)

        // Firebase setting kısmında bu anahtarı bulabilirsin.
        .requestIdToken(getString(R.string.default_web_client_id))
        .requestEmail()
        .build();

mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
// [END config_signin]

// [START initialize_auth]
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
// [END initialize_auth]

Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);

}

// [START on_start_check_user]
@Override
public void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    FirebaseUser currentUser = mAuth.getCurrentUser();
    updateUI(currentUser);
}
// [END on_start_check_user]

// [START onactivityresult]
@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);
            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);
        }
    }
}
// [END onactivityresult]

// [START auth_with_google]
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()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.d(TAG, "signInWithCredential:success");
                        FirebaseUser user = mAuth.getCurrentUser();
                        updateUI(user);
                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                        updateUI(null);
                    }
                }
            });
}
// [END auth_with_google]

// [START signin]
private void signIn() {
    Intent signInIntent = mGoogleSignInClient.getSignInIntent();
    startActivityForResult(signInIntent, RC_SIGN_IN);
}
// [END signin]

private void updateUI(FirebaseUser user) {

}

Lastly don't forget to add your SHA1 Fingerprint to firebase's console project. Here is the tutorial

// SHA1 FİNGİRPRİNT https://www.youtube.com/watch?v=ZPrKGjGDLtY