简体   繁体   English

Gmail API - 从新邮件下载附件所需的范围 - JAVA

[英]Gmail API - Download attachments from new mails required SCOPES - JAVA

I work on app that download attachments from new messages using GMAIL API .我在使用GMAIL API从新消息中下载附件的应用程序上工作。 I already made app with JavaMail but have to avoid using IMAP protocol.我已经使用JavaMail制作了应用程序,但必须避免使用IMAP协议。

I successfully logged into mail account with Oauth2 , but now I need help with fetching new mails and downloading attachments.我使用Oauth2成功登录了邮件帐户,但现在我需要帮助来获取新邮件和下载附件。 I saw example code where we search for messages by ID but its not usable in this situation.我看到了示例代码,其中我们按 ID 搜索消息,但在这种情况下它不可用。

EDIT 1: Code that I have is same as code from, just separated in classes Quickstart编辑 1:我拥有的代码与来自的代码相同,只是在类快速入门中分开

EDIT 2:编辑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:编辑 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.SCOPES使用GMAIL_READONLY替换GMAIL_LABELS ,我想我成功地获取了看不见的邮件,现在我可以以某种方式下载附件。

ListMessagesResponse listMessageResponse = service.users().messages().list(user).setQ("is:unseen").execute();

listMessageresponse is not null , while list equals null listMessageresponse不是null ,而list等于null

List<Message> list = listMessageResponse.getMessages();

You have a permission issue (as stated by the exception).您有权限问题(如例外所述)。 Permissions can be obtained by requesting a SCOPE when authenticating.认证时可通过请求 SCOPE 来获得权限。 If you are using the sample from the quickstart, you will see they are using the GMAIL_LABELS scope:如果您使用的是快速入门中的示例,您将看到他们使用的是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, ie GMAIL_READONLY or others scope allowing to access emails (see GmailScopes ).要获得 email 内容,您需要添加更多范围,即GMAIL_READONLY或其他 scope 允许访问电子邮件(请参阅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)根据您创建凭据/令牌的方式,您可能不允许进入这些范围(然后只需更新 API 设置中的设置以获取适当的范围)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM