0
votes

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();
1
Please provide the code you have so far.ziganotschka
You can list your emails specifying with the q parameter is:unread to fetch the new messages.ziganotschka

1 Answers

0
votes

You have a permission issue (as stated by the exception). Permissions can be obtained by requesting a SCOPE when authenticating. If you are using the sample from the quickstart, you will see they are using the GMAIL_LABELS scope:

private static final List<String> SCOPES = Collections.singletonList(GmailScopes.GMAIL_LABELS);

To get the email content you need to add more scopes, i.e. GMAIL_READONLY or others scope allowing to access emails (see GmailScopes).

Depending on how you create your credentials/token you might not be allowed to these scopes (then just update your settings in your API settings for the proper scopes)