I did setup the Service Account and downloaded the JSON file used for credential creation followed below link :
https://developers.google.com/identity/protocols/OAuth2ServiceAccount#callinganapi
The below code throws error if credential is created using the JSON file :
GoogleCredential credential1 = GoogleCredential.fromStream(new FileInputStream("Project-1234.json"))
.createScoped(Collections.singleton(GMAIL_SCOPE));
Instead if i create credential with P12 file it is working fine :
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(serviceAccountUserEmail)
.setServiceAccountPrivateKeyFromP12File(new File("Project-123.p12"))
.setServiceAccountScopes(Collections.singleton(GMAIL_SCOPE))
.setServiceAccountUser("sdkg1308@gmail.com")
.build();
This is the error i get when used the JSON file to create credential object :
com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Bad Request",
"reason" : "failedPrecondition"
} ],
"message" : "Bad Request"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
If google API doesn't support JSON format then why is it mentioned in the doc that it is the recommended approach to store the generated key for a service account.