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)