I work on app that download attachments from new messages using GMAIL API. I already made app with JavaMail but have to avoid using IMAP protocol.
I successfully logged into mail account with Oauth2, but now I need help with fetching new mails and downloading attachments. I saw example code where we search for messages by ID but its not usable in this situation.
EDIT 1 : Code that I have is same as code from, just separated in classes Quickstart
EDIT 2:
String user = "me";
ListMessagesResponse listMessageResponse = service.users().messages().list(user).setQ("is:unseen").execute();
List<Message> list = listMessageResponse.getMessages();
for(Message m : list) {
List<MessagePart> part = m.getPayload().getParts();
for(MessagePart p: part) {
if(p.getFilename()!=null && p.getFilename().length()>0) {
System.out.println(p.getFilename());
}
}
}
I get error for this line
ListMessagesResponse listMessageResponse = service.users().messages().list(user).setQ("is:unseen").execute();
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Insufficient Permission",
"reason" : "insufficientPermissions"
} ],
"message" : "Insufficient Permission"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:150)
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:451)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1089)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:549)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:482)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:599)
at Main.main(Main.java:64)
EDIT 3:
Made SCOPES
working with replacing GMAIL_LABELS
with GMAIL_READONLY
, I think I successfully fetched unseen mails, now I have somehow to download attachments.
ListMessagesResponse listMessageResponse = service.users().messages().list(user).setQ("is:unseen").execute();
listMessageresponse
is not null
, while list
equals null
List<Message> list = listMessageResponse.getMessages();
q
parameteris:unread
to fetch the new messages. – ziganotschka