1

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.

ssdg
  • 97
  • 1
  • 3
  • 12
  • Check this [SO question](http://stackoverflow.com/questions/29327846/gmail-rest-api-400-bad-request-failed-precondition), the answer here can help you to solve your problem. – KENdi Sep 12 '16 at 00:25
  • Thanks for the reply. I have gone through the given link. But it explains using P12 file which i have already a working example. I would like to achieve the same using .JSON file but unable to do so. – ssdg Sep 12 '16 at 22:38
  • I faced this error quite a few times and in my case, it was always with the time on my machine which was incorrect. – ND003 Sep 14 '16 at 10:28

0 Answers0