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.