亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 實現(大多數時候是不完整的),而且我花在升級客戶端上的時間比我想花的時間還長。所以我在這里復制我自己的答案??赡苡腥擞X得這很有用。


從 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 關注
  • 289 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號