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")
}
}
}