androidkotlincredentialsgoogle-identityandroid-credential-manager

Getting Integer value from googleIdTokenCredential.id but expected String


I want to create login system using 'GoogleIdTokenCredential'. But the problem is sometimes googleIdTokenCredential.id returns integer and googleIdTokenCredential.displayName returns null value.

But according to the documentation getId() is supposed to return a NonNull string.

Documentation said: public final @NonNull String getId()

the email address associated with user's Google Account.

My Code:

In build.gradle

dependencies {
     // dependencies for credential
    implementation("androidx.credentials:credentials-play-services-auth:1.2.2")
    implementation("com.google.android.libraries.identity.googleid:1.1.1")
}

In Fragment

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val googleIdOption by lazy {
            GetGoogleIdOption.Builder().setFilterByAuthorizedAccounts(false)
                .setServerClientId(serverClientId)
                .setAutoSelectEnabled(true).setNonce(generateNonce()).build()
        }

        val request: androidx.credentials.GetCredentialRequest =
            androidx.credentials.GetCredentialRequest.Builder().addCredentialOption(googleIdOption)
                .build()

        val mContext = requireContext()
        val credentialManager by lazy { androidx.credentials.CredentialManager.create(mContext) }

        lifecycleScope.launch {
            try {
                val result = credentialManager.getCredential(
                    request = request,
                    context = mContext,
                )
                handleSignIn(result)
            } catch (e: androidx.credentials.exceptions.GetCredentialException) {
                showMessage("Failed to get credential: $e")
            }
        }
    }

    private fun handleSignIn(result: androidx.credentials.GetCredentialResponse) {
        when (val credential = result.credential) {
            is CustomCredential -> {
                if (credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) {
                    try {
                        val googleIdTokenCredential =
                            GoogleIdTokenCredential.createFrom(credential.data)

                        val id = googleIdTokenCredential.id
                        val displayName = googleIdTokenCredential.displayName

                        if (displayName == null) {
                            showMessage("Login Failed\nid: $id")
                            return
                        }

                    } catch (e: GoogleIdTokenParsingException) {
                        showMessage("Received an invalid google id token response, $e")
                    }
                }
            }

            else -> {
                showMessage("Unexpected type of credential")
            }
        }
    }

    private fun generateNonce(): String {
        val nonceBytes = ByteArray(16)
        SecureRandom().nextBytes(nonceBytes)
        return Base64.encodeToString(
            nonceBytes, Base64.NO_PADDING or Base64.NO_WRAP or Base64.URL_SAFE
        )
    }

    private fun showMessage(message: String) {
        Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
    }

I have searched a lot on google search engine and stack overflow about this but didn't find any answer. Even asking 'artificial intelligence' doesn't get any correct answer.

Please note the below screen record, according to the code I am supposed to get the String (gmail) output of googleIdTokenCredential.id, but I am getting Integer. output of my code


Solution

  • You're right. googleIdTokenCredential.getID() should be returning the email address of the user.

    Google has identified the issue and will be correcting it soon.

    Source: https://issuetracker.google.com/issues/368873078?pli=1