google-api-java-client: Google Sign-In: GoogleIdTokenVerifier.verify returns null for a valid id token

Environment details

  1. OS type and version: Mac OS Ventura 13.3.1
  2. Java version: openjdk 17.0.6 2023-01-17 LTS
  3. version(s): 1.33.0

Description

I am integrating the Google sign-in and authenticating the id token on the backend as described here. The documentation for the GoogleIdTokenVerifier states to specify the client ids of apps (the mobile apps I believe) when instantiating the class, but when I call GoogleIdTokenVerifier.verify on the idToken (returned from the flow with the mobile app), it returns null (meaning the idToken is invalid). Specifying the web client id (which is for the server) however, works. Can this be clarified, please? Which client ids are meant to be specified when instantiating the class?

Steps to reproduce

Same steps as described here

Code example

val verifier = GoogleIdTokenVerifier.Builder(NetHttpTransport(), GsonFactory())
  .setAudience(/* mobile client ids */)
  .build()
verifier.verify(/* idToken from android */) // returns null instead of the GoogleIdToken instance

Stack trace

NA

External references such as API reference guides

https://developers.google.com/identity/sign-in/android/backend-auth

Any additional information below

NA

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (2 by maintainers)

Most upvoted comments

Finally got the thing to work, I should mention: .setAudience() seems to accept the web_client_id (for the front end) rather than the android_client_id (supposed to be for the backend), again this is highly likely because the aud and azp are mysteriously switched for some reason. Not only that, when using Arrays.asList() as long as one of the strings is the web client ID it will work but you can also just get rid of setAudience() together and it’ll work.

For those in the future who may be reading this:

After that, I was faced with a android.os.NetworkOnMainThreadException error, and I can’t be bothered to explain the reason so I’ll spoonfed my working code instead… Before:

GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport, jsonFactory)
    .setAudience(Collections.singleton(getString(R.string.web_client_id)))
    .build();

try {
    return verifier.verify(unverifiedIdToken);
    // Put code here to do after verification here

After:

ExecutorService executor = Executors.newSingleThreadExecutor();

Future<GoogleIdToken> future = executor.submit(() -> {
    GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(new NetHttpTransport(), new GsonFactory())
            .setAudience(Collections.singletonList("WEB_CLIENT_ID")) // or just get rid of the setAudience entirely for some reason
            .build();

    try {
        return verifier.verify(unverifiedIdToken);
    } catch (GeneralSecurityException | IOException e) {
        e.printStackTrace();
        return null;
    }
});

executor.execute(() -> {
    GoogleIdToken idToken = null;
    try {
        idToken = future.get(); // This will block until the task is done
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }

    // Put code here to do after verification here

    executor.shutdown(); // Shut down the executor after the task is done
});

Hopefully that helped someone… if only Google just updated their damn documentation haha

We have made the docs clearer on the fact that this is the web client token id. Please feel free to reopen if you think this still needs more clarity.

Well @idrisadetunmbi it’s been 2 weeks now, did you find a solution to this?

Hi @DaTigerYT. For now, I just specified the web client id (meant for the server) as part of the client ids when initializing the verifier.

The problem could also be from the client and not the verifier, but I have not had the time to verify this.