为了账号安全,请及时绑定邮箱和手机立即绑定

Microsoft Graph SDK Java:如何在包含 contentBytes

Microsoft Graph SDK Java:如何在包含 contentBytes

幕布斯7119047 2023-06-14 15:32:45
我正在尝试使用 Microsoft Graph SDK 获取 contentBytes 以下载办公邮件附件。我尝试了以下但它给出了 ClassCastException。我不知道如何通过 Microsoft Graph SDK(Java) 直接获取 FileAttachment 对象列表IAttachmentCollectionPage aAttachementPage = clientAuthGraphServiceClient.users("#userIDGiven#").mailFolders("Inbox").messages(message.id).attachments().buildRequest().get();for (Attachment aAttachment:aAttachementPage.getCurrentPage()) {   String serviceDirectory="D:\\DOWNLOADS\\";   File fileDown= new File(serviceDirectory+aAttachment.name);   Files.write(fileDown.toPath(), ((FileAttachment)aAttachment).contentBytes);}我正在使用以下罐子:    <dependency>        <groupId>com.microsoft.graph</groupId>        <artifactId>microsoft-graph</artifactId>        <version>1.4.0</version>    </dependency>
查看完整描述

4 回答

?
PIPIONE

TA贡献1829条经验 获得超9个赞

对于 GraphApi 版本 2.5.0

AttachmentCollectionPage attachmentCollectionPage =
    graphClient
        .me()
        .messages(messageId)
        .attachments()
        .buildRequest()
        .get();    JsonElement att = attachmentCollectionPage
        .getCurrentPage()
        .get(0)
        .getRawObject()
        .get("contentBytes"); 
               byte[] decodedBytes = Base64.getDecoder().decode(att.getAsString());


查看完整回答
反对 回复 2023-06-14
?
慕标5832272

TA贡献1966条经验 获得超4个赞

每隔一段时间,我都会对着 Microsoft 文档大骂 Java 实现(大多数时候是不完整的),而且我花在升级客户端上的时间比我想花的时间还长。所以我在这里复制我自己的答案。可能有人觉得这很有用。


从 microsoft-graph 版本 5.34.0 开始,不再需要一些东西,比如解码,您将能够简单地转换您的对象。


获取 File/ItemAttachments 的复杂方法:


List<Attachment> attachments = new ArrayList<>();


        try {

            AttachmentCollectionPage mgAttachmentList =

                    graphClient.users(emailAddress)

                            .messages(mail.id)

                            .attachments()

                            .buildRequest()

                            .get();


            do {

                attachments.addAll(mgAttachmentList.getCurrentPage());


                // Check for next page

                if (mgAttachmentList.getNextPage() != null) {

                    mgAttachmentList = mgAttachmentList.getNextPage().buildRequest().get();

                } else {

                    mgAttachmentList = null;

                }

            } while (mgAttachmentList != null);


            attachments.stream()

                    .filter(Objects::nonNull)

                    .forEach(attachment -> {

                        try {

                            // attachment equals a FileAttachment

                            if ("#microsoft.graph.fileAttachment".equalsIgnoreCase(attachment.oDataType)) {

                                byte[] attachmentContentBytes = ((FileAttachment) attachment).contentBytes;

                                if (attachmentContentBytes != null) {

                                    // your own logic here

                                }

                            } else if ("#microsoft.graph.itemAttachment".equalsIgnoreCase(attachment.oDataType)) {

                                /*

                                 *  We need to convert each Attachment.

                                 *  If it's an ItemAttachment, we need to do another call to retrieve the item-object.

                                 *

                                 *  The standard call with expand option gives us an Attachment (again)

                                 */

                                if (StringUtils.isNotBlank(attachment.contentType)) {

                                    Attachment attachment = graphClient.users(emailAddress)

                                            .messages(mail.id)

                                            .attachments(attachment.id)

                                            .buildRequest()

                                            .expand("microsoft.graph.itemattachment/item")

                                            .get();

                                    if (attachment != null) {

                                        // cast the object to whatever you need, in this example I retrieve the webLink

                                        String linkToMessage = ((Message) ((ItemAttachment) attachment).item).webLink;

                                        // your own logic here

                                    }

                                } else {

                                    log.info("Unknown attachment contentType for an ItemAttachment - Could not read attachment.");

                                }

                            } else {

                                log.error("Unable to read an attachment for mail.");

                            }

                        } catch (Exception e) {

                            log.error("Could not read attachment: '" + attachment.name + "' for message: '" + message.subject + ". " + e.getMessage(), e);

                        }

                    });

        } catch (Exception e) {

            log.error("Something went wrong while receiving attachments for message: '" + message.subject + "'. " + e.getMessage(), e);

        }


查看完整回答
反对 回复 2023-06-14
?
交互式爱情

TA贡献1712条经验 获得超3个赞

我知道这是一个老问题,但我偶然发现了这个问题并认为我可以分享我们的解决方案。注意 NPE 或 IndexOutOfBoundsExceptions。


    MessageCollectionPage messageCollectionPage = graphClient.me().messages().buildRequest().get();


    List<String> messageIdsWithAttachments =

        messageCollectionPage.getCurrentPage().stream()

            .filter(message -> message.hasAttachments)

            .map(message -> message.id)

            .toList();


    AttachmentCollectionPage attachmentCollectionPage =

        graphClient

            .me()

            .messages(messageIdsWithAttachments.get(0))

            .attachments()

            .buildRequest()

            .get();


    byte[] bytes = ((FileAttachment) attachmentCollectionPage.getCurrentPage().get(0)).contentBytes;


查看完整回答
反对 回复 2023-06-14
?
烙印99

TA贡献1829条经验 获得超13个赞

有几种派生类型的附件。

  • 文件附件

  • 项目附件

  • 参考附件

如果任何附件不是 FileAttachment,那么您将得到这种强制转换异常。在尝试将其转换为 FileAttachment 之前,您需要测试附件的类型。


查看完整回答
反对 回复 2023-06-14
  • 4 回答
  • 0 关注
  • 143 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信