androidx.credentials.exceptions.GetCredentialCancellationException: activity is cancelled by the user

526 Views Asked by At

Trying to login with the Google Button in my android app using the credential manager, but the login shows the activity is cancelled by the user while choosing the gmail account from the popup. But it shows the error as androidx.credentials.exceptions.GetCredentialCancellationException: activity is cancelled by the user.

Here I have used the web_client_id in the GCP and there is two Android ClientID for the same app.

So I have deleted the AppSigningKey (SHA-1) available client_id and kept the SHA-1 of the already available (i.e) the upload keystore (SHA-1)

val googleIdOption: GetSignInWithGoogleOption = GetSignInWithGoogleOption.Builder(WEB_CLIENT_ID).build()
val request: GetCredentialRequest = GetCredentialRequest.Builder().addCredentialOption(googleIdOption).build()

 R.id.btn_googleSignUp -> {
                val credentialManager = CredentialManager.create(this@SignUpActivity)
                coroutineScope.launch {
                    try {
                        Log.e("request.. ===", request.toString())
                        val result = credentialManager.getCredential(
                            request = request,
                            context = this@SignUpActivity,
                        )
                        handleSignIn(result)
                    } catch (e: GetCredentialCancellationException) {
                        Log.e("CredentialCancelException ===", e.message.toString())
                    } catch (e: GetCredentialException) {
                        Log.e("GetCredential ===", e.message.toString())
                    } catch (e: Exception) {
                        Log.e("Exception ===", e.message.toString())
                    }
                }
            }

fun handleSignIn(result: GetCredentialResponse) {
        // Handle the successfully returned credential.
        val credential = result.credential
        when (credential) {
            is PublicKeyCredential -> {
                // Share responseJson such as a GetCredentialResponse on your server to
                // validate and authenticate
               //responseJson = credential.authenticationResponseJson
            }

            is PasswordCredential -> {
                // Send ID and password to your server to validate and authenticate.
                val username = credential.id
                val password = credential.password
            }

            is CustomCredential -> {
                if (credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) {
                    try {
                        // Use googleIdTokenCredential and extract id to validate and
                        // authenticate on your server.
                        val googleIdTokenCredential = GoogleIdTokenCredential
                            .createFrom(credential.data)
                    } catch (e: GoogleIdTokenParsingException) {
                        Log.e("Invalid GoogleId", "Received an invalid google id token response", e)
                    }
                } else {
                    // Catch any unrecognized custom credential type here.
                    Log.e("Unexpected", "Unexpected type of credential")
                }
            }

            else -> {
                // Catch any unrecognized credential type here.
                Log.e("Invalid GoogleID", "Unexpected type of credential")
            }
        }
    }
1

There are 1 best solutions below

0
On

Okay I found the error for me

Before starting I want to note that this kind of error I as investigated tipically doesn't have relation with the android code itself, is more about the CLIENT_SERVER_ID you are using and the Google Cloud credentials.

My Solution:

I was creating the GetSignInWithGoogleOption using the WEB_SERVER_ID (not the android ID) and that's correct, but it's also necessary to have created the android credential in Google Cloud Console for the app running

In my case I had an "automatically created by Google Service" Android credential and I thought that was correct but it was created for another developer in the past, so I needed to create one for my own

So I went to Google Cloud Console and tap Create Credential -> OAuth Client ID and created an "Android application type". Package is the package name you can find in the manifest.xml and the SHA1 is a debug one (as i don't have a production one). That SHA1 for your device could be found using this command (replace the path):

keytool -keystore path-to-debug-keystore -list -v

For MAC would be:

keytool -keystore ~/.android/debug.keystore -list -v

The password is "android" for that debug keystore

The documentation that helped with the problem is this: https://support.google.com/cloud/answer/6158849?hl=es-419#installedapplications&android&zippy=%2Cnative-applications%2Candroid