简体   繁体   中英

Google Sign-in in android app not working now

I was developing an android app which uses google sign-in process (which was already integrated into my app). It worked for me some months ago but doesn't work now(google updates some policies or whatever!). Can anyone suggest what is going on here? Thanks in advance. Here's my code for integrating google sign-in process:

public class login extends AppCompatActivity {

private SignInButton signInButton;
private TextView textView, signT;
private static final int SIGNIN_CODE = 1;
private GoogleApiClient mGoogleApiClient;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

private final static String TAG = "Login Info:";

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

    mAuth = FirebaseAuth.getInstance();
    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            if (firebaseAuth.getCurrentUser() != null) {
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                i.putExtra("Val", "That i need");
                startActivity(i);
                overridePendingTransition(R.anim.fadein, R.anim.fadeout);
                finish();
            }
        }
    };

    signInButton = findViewById(R.id.SignIn);

    signT = (TextView) signInButton.getChildAt(0);
    signT.setText("Sign in with Google");

    textView = findViewById(R.id.negativetext);
    textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
    textView.setText(getString(R.string.negativetext));

    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(i);
            overridePendingTransition(R.anim.fadein, R.anim.fadeout);
            finish();
        }
    });

    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, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                    Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
                }
            })
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

    signInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            signIn();
        }
    });

}


@Override
protected void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
}

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

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SIGNIN_CODE) {
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            // Authenicate with my Firebase
            GoogleSignInAccount account = task.getResult(ApiException.class);
            ProgressDialog dialog = new ProgressDialog(this); //ProgressDialog.show(this, null, "Please wait while we load sign in..", true);
            dialog.setMessage("Please wait while we load sign in..");
            dialog.setIndeterminate(true);
            Drawable drawable = new ProgressBar(this).getIndeterminateDrawable().mutate();
            drawable.setColorFilter(ContextCompat.getColor(this, R.color.progress),
                                    PorterDuff.Mode.SRC_IN);
            dialog.setIndeterminateDrawable(drawable);
            dialog.show();

            firebaseAuthWithGoogle(account);
        } catch (ApiException e) {
            // Google Sign In failed?
            Log.w(TAG, "Google sign in failed", e);
        }
    }
}

private void firebaseAuthWithGoogle(final GoogleSignInAccount acct) {

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);

    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    //Debugging :(
                    //Toast.makeText(getApplicationContext(), "" + task.isSuccessful(), Toast.LENGTH_LONG).show();
                    if (task.isSuccessful()) {
                        //spinner.setVisibility(ProgressBar.INVISIBLE);
                        Log.i(TAG, "signInWithCredential:success");
                    } else {
                        Toast.makeText(getApplicationContext(), "We couldn't load sign in", Toast.LENGTH_LONG).show();
                    }
                }
            });
}}

Use below code, i already used it and it works perfectely for me:

private void configureGoogleSignIn() {
 GoogleSignInOptions googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(BuildConfig.DEFAULT_WEB_CLIENT_ID)       
            .requestEmail()
            .build();
googleSignInClient = GoogleSignIn.getClient(context, googleSignInOptions);
}

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

and onActivityResult

 @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        Task<GoogleSignInAccount> signInAccountTask = GoogleSignIn.getSignedInAccountFromIntent(data);
        handleSignInResult(signInAccountTask);
    } else {

    }
}

For more help you can go through my below sample link : https://github.com/rishavsingla/FirebaseAuthenticationSample

I don't know what problem occurred and gave me error code 12500, I visited google documentation & found no solution for error code 12500. So, I deleted my previous firebase authentication entry in console.firebase.google.com and then re-pasted the newly created google-services.json file into the app directory, and the rebuild.

That finally fixed my horrible issues!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM