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

2

6

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")
            }
        }
    }
Bowdlerize answered 26/12, 2023 at 12:50 Comment(1)
Hi! Could you fix it? Same errorAugur
A
7

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

Augur answered 29/12, 2023 at 16:15 Comment(2)
Yeah, you're right. When I added WebClient creds it works!Ghostly
Yes, WebClient Id also worked for me, But why isn't it working with android id?Outride
R
0

Create a OAuth 2.0 Client IDs android and use debug keystore SHA1 fingerprint, and you use web application Client ID, and it will work and give you token

keytool -list -v -keystore C:\Users\YourUserName.android\debug.keystore -alias androiddebugkey -storepass android -keypass android

Here is the complete code

private CredentialManager credentialManager;
   

     credentialManager = CredentialManager.create(getContext());

 

     runGoogleLogin("ClientId");
    
        private void runGoogleLogin(@NonNull String clientId) {
            GetSignInWithGoogleOption googleIdOption = new GetSignInWithGoogleOption.Builder(clientId).build();
            GetCredentialRequest request = new GetCredentialRequest.Builder().addCredentialOption(googleIdOption).build();
            credentialManager.getCredentialAsync(getContext(), request, null, THREAD_POOL_EXECUTOR, new CredentialManagerCallback<GetCredentialResponse, GetCredentialException>() {
                @Override
                public void onResult(GetCredentialResponse getCredentialResponse) {
                    GoogleIdTokenCredential credential = (GoogleIdTokenCredential) getCredentialResponse.getCredential();
                    String idToken = credential.getIdToken();
    //                loginSignUpSocialApi(idToken, "GOOGLE");
                }
                @Override
                public void onError(@NonNull GetCredentialException e) {
                    Log.e("Googleerror", e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Functions.showToast(getContext(), e.getMessage(), FancyToast.ERROR);
                        }
                    });
                }
            });
        }
Rustler answered 22/8 at 19:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.