firebase / snippets-android

Android snippets for firebase.google.com
Apache License 2.0
764 stars 398 forks source link

I am getting com.google.android.gms.common.api.ApiException: 4: 4: #540

Open SUMIT0733 opened 5 months ago

SUMIT0733 commented 5 months ago

I am using Firebase auth to signin user with Google, and I have setup the Firebase project with proper configuration and google-services.json and add that to my android project. I have added below dependencies in my build-gradle.kts(project) id("com.google.gms.google-services") version "4.4.1" apply false build.gradle.kts(app) implementation("com.google.firebase:firebase-auth:22.3.1") implementation(platform("com.google.firebase:firebase-bom:32.8.0")) implementation("com.google.android.gms:play-services-auth:21.0.0")

This is my mainactivity.ky

` private fun signIn(){ val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(getString(R.string.default_web_client_id)) .requestEmail() .build() googleSignInClient = GoogleSignIn.getClient(this, gso) val signInIntent = googleSignInClient.signInIntent startActivityForResult(signInIntent, SIGNIN_CODE) } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data)

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == SIGNIN_CODE) {
        val task = GoogleSignIn.getSignedInAccountFromIntent(data)
        try {
            // Google Sign In was successful, authenticate with Firebase
            val account = task.getResult(ApiException::class.java)!!
            Log.d(TAG, "firebaseAuthWithGoogle:" + account.id)
            firebaseAuthWithGoogle(account.idToken!!)
        } catch (e: ApiException) {
            // Google Sign In failed, update UI appropriately
            Log.w(TAG, "Google sign in failed", e)
        }
    }
}

private fun firebaseAuthWithGoogle(idToken: String) {
    val credential = GoogleAuthProvider.getCredential(idToken, null)
    auth.signInWithCredential(credential)
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                // Sign in success, update UI with the signed-in user's information
                Log.d(TAG, "signInWithCredential:success")
                val user = auth.currentUser
                Toast.makeText(this, "Login Successfull", Toast.LENGTH_SHORT).show()
            } else {
                // If sign in fails, display a message to the user.
                Log.w(TAG, "signInWithCredential:failure", task.exception)
                Toast.makeText(this, "Login Failed", Toast.LENGTH_SHORT).show()
            }
        }
}

This is giving me random results, I am able to log in successfully sometimes, and sometimes I am gettingcom.google.android.gms.common.api.ApiException: 4: 4: ` exception, I am not sure what is wrong, I tried everything, to add value ofdefault_web_client_id to string.xml to remove that, and invalidate and restart my projects too.

SUMIT0733 commented 5 months ago

Requirement changed.