0

I am trying to use GMAIL API to download attachments from my email account using OAuth 2.0 server to server Application. Here is my code:

    private static final String USER = "[email protected]";
    private static String emailAddress = "[email protected]";
    {
       try {
              httpTransport = GoogleNetHttpTransport.newTrustedTransport();
              credential = new GoogleCredential.Builder()
              .setTransport(httpTransport)
              .setJsonFactory(JSON_FACTORY)
              .setServiceAccountId(emailAddress)
              .setServiceAccountPrivateKeyFromP12File(new File("myfile.p12"))
              .setServiceAccountScopes(Collections.singleton(GmailScopes.GMAIL_READONLY))
              .build();
          }
          catch (IOException | GeneralSecurityException e)
          {
              throw new RuntimeException(e);
          }
    }
    // Create a new authorized Gmail API client
    Gmail service = new Gmail.Builder(httpTransport, JSON_FACTORY, credential)
        .setApplicationName(APP_NAME).build();

    // Retrieve a page of Threads
    ListThreadsResponse threadsResponse = service.users().threads().list(USER).execute();
      List<Thread> threads = threadsResponse.getThreads();

   // Print ID of each Thread.
    for (Thread thread : threads) {
      System.out.println("Thread ID: " + thread.getId());

}

I get the error at the threadsResponse call. These are the errors:

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 500 Internal Server Error.

{ "code" : 500, "errors" : [ { "domain" : "global", "message" : "Backend Error", "reason" : "backendError" } ], "message" : "Backend Error" } 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:312) at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1049) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460) at core.crescent.gmail.GmailApiQuickstart.main(GmailApiQuickstart.java:192)

2
  • If you want to access a single user, you can use authentication for single user instead of service account. I don't think gmail API supports service account. stackoverflow.com/questions/24523003/… Commented Jan 5, 2015 at 22:39
  • I am using OAuth2 Server to server application link because it seems like it's the only way to get authorization to access a gmail account without a user consent (without having to open browsers and do manual consent). Is there another way to accomplish this. Thank you Commented Jan 12, 2015 at 19:06

2 Answers 2

0

You could retry the request, with exponential backoff. Worked for me. See https://developers.google.com/drive/web/handle-errors

Sign up to request clarification or add additional context in comments.

Comments

0

So I figured out the issue and fixed it!

When setting up "Credential" (in the code I posted above). I needed to add .setServiceAccountUser(USER) sub-routine. By doing this, you make the link between the service account created and the GMAIL account you want to access. I saw it earlier in https://developers.google.com/accounts/docs/OAuth2ServiceAccount, but I was under the impression that it was only if you wanted to link with an account under a different domain name.

This fixed my issues. Thank you all for your responses

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.