简体   繁体   中英

Google API Exception “invalid_grant” in DEVs GlassFish but not in Local

I'm working using OAuth 2.0 to authenticate to a Google's API and everything works just fine in my local GlassFish. The problem begins when I'm deploying to the DEV's GlassFish, which throws me the following exception:

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request { "error" : "invalid_grant" }

This is my code:

        transport = GoogleNetHttpTransport.newTrustedTransport();
        jsonFactory = JacksonFactory.getDefaultInstance();

        credential = new GoogleCredential.Builder()
                .setTransport(transport)
                .setJsonFactory(jsonFactory)
                .setServiceAccountUser(googleAdminEmail)
                .setServiceAccountId(googleAdminAccount)
                .setServiceAccountScopes(Arrays.asList(
                                DirectoryScopes.ADMIN_DIRECTORY_USER,
                                DirectoryScopes.ADMIN_DIRECTORY_GROUP,
                                DirectoryScopes.ADMIN_DIRECTORY_GROUP_MEMBER))
                .setServiceAccountPrivateKeyFromP12File(new File(googleAdminKeyPath))
                .build();           

        credential.refreshToken();

The exact line that throws me the exception is:

 credential.refreshToken();

If I get the Access Token from my local run, let's say the token value is "usghiudblk", and I set that value when running on DEV's GlasshFish: credential.setAcessToken("usghiudblk"); it will let me access normally but still this won't work: credential.refreshToken();

I truly can't tell why everything works just fine in local but doesn't works in DEV's GlassFish.

Solved it by the answer on this post:

Invalid grant when accessing Google API

The problem was that the server's clock was not synchronized with NTP.

That was it...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM