Why does Credential Manager return a numeric ID instead of email for some Gmail accounts?

Hey everyone, I’m stuck with a weird problem in my Android app. I’m using the Google Credential Manager for sign-ins, and it’s mostly working fine. But here’s the issue:

When I log in with my main Gmail, everything’s cool. The credential.getId() gives me the email address like it should. But when I use my other Gmail account, it spits out some long number instead of the email. It looks like a Google ID or something.

I thought the getId() method was supposed to always give the email address. That’s what the docs say, right?

Here’s a simplified version of what I’m doing:

val credRequest = CredRequest.Builder()
    .addGoogleSignInOption(googleSignInOption)
    .build()

val cred = credManager.getCred(activity, credRequest)
println("ID: ${cred.credential.id}")

I’m using the latest beta of the credentials library. Any ideas why this is happening with just one account? Is there a way to always get the email instead of this ID? Thanks for any help!

I’ve run into this exact problem before. It’s frustrating, but there’s a workaround. The numeric ID you’re seeing is likely the user’s Google account ID. Some accounts, particularly older ones or those set up through work/school, behave differently.

To reliably get the email, you can use the GoogleSignInClient instead. Here’s what worked for me:

val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
    .requestEmail()
    .build()
val googleSignInClient = GoogleSignIn.getClient(activity, gso)

val signInIntent = googleSignInClient.signInIntent
startActivityForResult(signInIntent, RC_SIGN_IN)

// In onActivityResult:
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
val account = task.getResult(ApiException::class.java)
val email = account?.email

This method has been more consistent in my experience. Just remember to handle exceptions and null values appropriately in your production code.

hey mate, i had a similar issue. turns out some accounts (esp older ones) do this weird ID thing. Try using GoogleSignInAccount instead of Credential Manager. It’s more reliable for getting emails consistently. Good luck with ur app!

I’ve encountered this issue before, and it’s actually tied to how Google handles different account types. Some Gmail accounts, especially older ones or those created through G Suite (now Google Workspace), may return a numeric ID instead of an email address.

To consistently get the email, you might want to use the GoogleSignInAccount object. After getting the credential, you can do:

val googleCredential = auth.getCredentialFromIntent(cred.credential)
val googleIdToken = googleCredential.googleIdToken
val googleSignInAccount = GoogleSignIn.getSignedInAccountFromIntent(googleIdToken).result
val email = googleSignInAccount.email

This approach should reliably fetch the email regardless of the account type. Keep in mind that you’ll need to handle potential exceptions and null checks in a production environment.