androidandroid-studiofirebase-authenticationonactivityresult

Google SignIn using Intent ActivityResult always with RESULT_CANCELED


Google SignIn using Intent gives resultCode always 0 of RESULT_CANCELLED, I don't know whats the problem here.


class MainActivity : AppCompatActivity() {

    private lateinit var googleSignInClient : GoogleSignInClient

    private lateinit var googleSignIn : ActivityResultLauncher<Intent>

    override fun onCreate(savedInstanceState : Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        onResult()

        // Configure Google Sign In
        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.client_id))
                .requestEmail()
                .build()

        googleSignInClient = GoogleSignIn.getClient(this, gso)


        findViewById<Button>(R.id.btnSignUp).setOnClickListener {
            val signInIntent : Intent = googleSignInClient.signInIntent
            googleSignIn.launch(signInIntent)
        }

    }


    private fun onResult() {
        googleSignIn = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            Log.e("onResult", "${it.resultCode}")
        }
    }


}

In Log cat resultcode is 0 which is RESULT_CANCELLED

2021-05-25 08:02:34.088 26817-26817/com.right.tools E/onResult: 0

Solution

  • I'm having same issue and after spending a day i figured it out generally RESULT_CANCELLED occurs when client_id is incorrect make sure you enter web_client_id not android_client_id in requestIdToken/setServerClientId.

    If you are implementing in android you need to create both web and android client id from developer console.