I am trying to modify labels of a message by implementing a server to server authentication, so I need the scope GMAIL_MODIFY which will give me permission to do other things (Read/write). When I do implement this change, I get a NullPointerException as soon I try the first request.
For the sake of simplicity, I'm using a simple code of listing threads from my gmail account.
I did setup a project on my account's developers console and all of that jazz.
Here is my Java code:
public static void main (String [] args) throws IOException {
Collection<String> scopesArray = new ArrayList<String>();
scopesArray.add(GmailScopes.GMAIL_MODIFY);
//scopesArray.add(GmailScopes.GMAIL_READONLY);
{
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(email_address)
.setServiceAccountPrivateKeyFromP12File(new File("myfile.p12"))
.setServiceAccountScopes(scopesArray)
.setServiceAccountUser(USER)
.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();
try {
ListThreadsResponse threadsResponse = service.users().threads().list(USER).execute();
List<com.google.api.services.gmail.model.Thread> threads = threadsResponse.getThreads();
for (com.google.api.services.gmail.model.Thread thread : threads) {
System.out.println("Thread ID: " + thread.getId());
}
} catch (GoogleJsonResponseException e) {
GoogleJsonError error = e.getDetails();
System.err.println("Error code: "+ error.getCode());
System.err.println("Error message: " + error.getMessage());
} catch (HttpResponseException e) {
// No Json body was returned by the API.
System.err.println("HTTP Status code: "+ e.getStatusCode());
System.err.println("HTTP Reason: " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
}
this returns: Exception in thread "main" java.lang.NullPointerException at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191) at com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127) at com.google.api.client.json.jackson2.JacksonFactory.createJsonParser(JacksonFactory.java:96) at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:85) at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81) at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:88) at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287) at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307) at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:268) at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489) at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217) at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859) 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:228)
When I use the scope GMAIL_READONLY scope it works. Why doesn't it work with the scope modify even though according to the GMAIL API documentation it supposed to work (https://developers.google.com/gmail/api/v1/reference/users/threads/list)?
Thank you