google-apigoogle-oauth-java-client

Unable to get access token with GoogleCredentials


I'm trying to get access token using my service account JSON file. However I'm unable to get access token.

GoogleCredential googleCredential = GoogleCredential.fromStream(new FileInputStream(serviceAccountJsonFile));
System.out.print(googleCredential.getAccessToken());

I get null every time. I am able to use the same credentials with Python APIs.

Can someone point me if I'm missing something here.


Solution

  • Got it working with the below code:

    GoogleCredential googleCredential = GoogleCredential.fromStream(new FileInputStream(serviceAccountJsonFile),  GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance());