GoogleSignInResult isSuccess failed

ⅰ亾dé卋堺 提交于 2019-11-28 11:42:49

We had same problem Error 12500, nothing help... Code was ok, SHA-1 keys ok, checked a lot of times... Spend two days with this f**king issue...

But we didn't have accepted OAuth consent on page https://console.developers.google.com/apis/credentials/consent?project=[PROJECT_ID] That help, just accept and fill page with OAuth consent, and it start working without any issue after.

I was using Google plus login successfully but faced same problem after changing the laptop I used for developing the app earlier.
Reason was change in SHA-1 registered on Google Developer Console.

So I'd recommend re-iterating the integration guide and fixing google-services.json (Configuration file) located in your app folder. Because problem could be with your in API keys, APIs enabled or the SHA-1.

Calling the connect() function after the "sing-in" code worked for me.

     Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(sActivity.mGoogleApiClient);
    sActivity.startActivityForResult(signInIntent, RC_SIGN_IN);

    mGoogleApiClient.connect(); //Adding this worked for me!

Few things to check:

1. Check your correct SHA certificate fingerprints in Firebase Console.

2. Check below warning in Google API Console.

  • If you see that error, You need to choose Support email in OAuth consent screen tab like an example below.

After chosen a support email, the warning disappeared. and it started working.

The problem is due to the Signing Certificate and the SHA-1 certificate fingerprint. Add the following SHA-1 certificates into your googleApi credentials. there are 2 cases

1.If you are running in debug mode add the SHA-1 fingerprint generated by the following

"C:\Program Files\Java\jre1.8.0_101\bin\keytool" -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

2.If you have configured Signing Config then use SHA-1 fingerprint generated by following

"C:\Program Files\Java\jre1.8.0_101\bin\keytool" -list -v -keystore "[youKeyPath]\youKey.jks"

I recommend you to add both the SHA-1 fingerprints in your googleApi credentials

[https://developers.google.com/identity/sign-in/android/sign-in ] follow this api documentation but keep in mind that inside WEB_CLIENT_ID use the value of client id which is generated inside google-services.json file.

class MainActivity : AppCompatActivity(), GoogleApiClient.OnConnectionFailedListener {

private val TAG = "JSAGoogleSignIn"
private val REQUEST_CODE_SIGN_IN = 1234
//  private val WEB_CLIENT_ID = "969826080188-rs95o2iilt1l9u6g1su0hf271ml4d5bj.apps.googleusercontent.com"
private val WEB_CLIENT_ID = "354298333018-hh9bnkfuf0meimf2n2u0pio7dh8mg273.apps.googleusercontent.com"
private var mAuth: FirebaseAuth? = null

private var mGoogleApiClient: GoogleApiClient? = null
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    var txt_register = findViewById<TextView>(R.id.txt_register)
    txt_register.setOnClickListener {
        var intent = Intent(this@MainActivity, RegisterActivity::class.java)
        finish()
        startActivity(intent)
    }
    val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(WEB_CLIENT_ID)
            .requestEmail()
            .build()
    mGoogleApiClient = GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build()

    mAuth = FirebaseAuth.getInstance()
    sign_in_button.setOnClickListener {
        val intent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient)
        startActivityForResult(intent, REQUEST_CODE_SIGN_IN)
    }

}


override fun onConnectionFailed(p0: ConnectionResult) {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}


private fun updateUI(user: FirebaseUser?) {
    if (user != null) {
        Log.e("Email", "Value" + user.email)
    }

}

fun signIn() {

}

override fun onStart() {
    super.onStart()
    val currentUser = mAuth!!.currentUser
    updateUI(currentUser)
}

public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
    if (requestCode == REQUEST_CODE_SIGN_IN) {
        val result = Auth.GoogleSignInApi.getSignInResultFromIntent(data)
        if (result.isSuccess) {
            // successful -> authenticate with Firebase
            val account = result.signInAccount
            firebaseAuthWithGoogle(account!!)
        } else {
            // failed -> update UI
            updateUI(null)
            Toast.makeText(applicationContext, "SignIn: failed!" + result.status,
                    Toast.LENGTH_SHORT).show()
        }
    }
}

private fun firebaseAuthWithGoogle(acct: GoogleSignInAccount) {
    Log.e(TAG, "firebaseAuthWithGoogle():" + acct.id!!)

    val credential = GoogleAuthProvider.getCredential(acct.idToken, null)
    mAuth!!.signInWithCredential(credential)
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    // Sign in success
                    Log.e(TAG, "signInWithCredential: Success!")
                    val user = mAuth!!.currentUser
                    updateUI(user)
                } else {
                    // Sign in fails
                    Log.w(TAG, "signInWithCredential: Failed!", task.exception)
                    Toast.makeText(applicationContext, "Authentication failed!",
                            Toast.LENGTH_SHORT).show()
                    updateUI(null)
                }
            }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!