Java – Google Sign In GoogleIdToken backend validation suddenly failed

Google Sign In GoogleIdToken backend validation suddenly failed… here is a solution to the problem.

Google Sign In GoogleIdToken backend validation suddenly failed

I’m having this issue all the time when signing in with Google. I have an Android app that users connect use to authenticate with Google and then send idToken to my server. The server uses a library provided by Google (GoogleIdTokenVerifier) to validate the token.

   GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport, jsonFactory)
            .setAudience(audience)
            .setIssuer("https://accounts.google.com")
            .build();

GoogleIdToken idToken = null;

try {
        idToken = verifier.verify(idTokenString);
    } catch (Exception e) {
        e.printStackTrace();
    }

if (idToken != null) {
        GoogleIdToken.Payload payload = idToken.getPayload();
        String userId = payload.getSubject();
        System.out.println("User ID: " + userId);
        String email = payload.getEmail();
        System.out.println("Emaail:" + email);
        return userId;
    } else {
        System.out.println("Invalid ID token.");
        return null;
    }

This worked for a while, and then the validation suddenly started always failing. Nothing has changed!
Any ideas?

Solution

Check your server time, I’m having the same issue when migrating to a new server. I solved this problem by setting the time zone using NTP.

Related Problems and Solutions