[英]How to retrieve office 365 mail file (like image,text file etc) attachment using Oauth Graph Service Client api in java?
這是我如何獲取附加到消息的附件對象列表:
IAttachmentCollectionRequest attachmentsPage = graphClient
.users(emailServer.getEmailAddress())
.mailFolders("Inbox")
.messages(mail.id)
.attachments()
.buildRequest();
List<Attachment> attachmentsData = attachmentsPage.get().getCurrentPage();
List<AttachmentData> attachmentDataToStore = new java.util.ArrayList<AttachmentData>();
for(Attachment attachment : attachmentsData)
{
attachmentData.setInputStream(
new ByteArrayInputStream(
attachment.getRawObject()
.get("contentBytes")
.getAsString()
.getBytes()));
}
現在,我相信內容字節到輸入 stream 的轉換沒有正確發生,最終 data(image.png) 被損壞。 有什么建議么?
使用FileAttachment
類為您處理所有這些。 不幸的是,SDK 並沒有以最“干凈”的方式處理這個問題——您必須再次請求附件。
public static void saveAttachments(String accessToken) {
ensureGraphClient(accessToken);
final String mailId = "message-id";
// Get the list of attachments
List<Attachment> attachments = graphClient
.me()
.messages(mailId)
.attachments()
.buildRequest()
// Use select here to avoid getting the content in this first request
.select("id,contentType,size")
.get()
.getCurrentPage();
for(Attachment attachment : attachments) {
// Attachment is a base type - need to re-get the attachment as a fileattachment
if (attachment.oDataType.equals("#microsoft.graph.fileAttachment")) {
// Use the client to generate the request URL
String requestUrl = graphClient
.me()
.messages(mailId)
.attachments(attachment.id)
.buildRequest()
.getRequestUrl()
.toString();
// Build a new file attachment request
FileAttachmentRequestBuilder requestBuilder =
new FileAttachmentRequestBuilder(requestUrl, graphClient, null);
FileAttachment fileAttachment = requestBuilder.buildRequest().get();
// Get the content directly from the FileAttachment
byte[] content = fileAttachment.contentBytes;
try (FileOutputStream stream = new FileOutputStream("C:\\Source\\test.png")) {
stream.write(content);
} catch (IOException exception) {
// Handle it
}
}
}
}
您從 rawObject JSON 讀取 contentBytes 的方法將起作用。 您只需要使用 Base64 解碼器而不是String.getBytes()
因為 contentBytes 是 base64 編碼的。 這在我的網絡上比使用 FileAttachmentRequestBuilder 再次請求附件要快一些。
IAttachmentCollectionRequest attachmentsPage = graphClient
.users(emailServer.getEmailAddress())
.mailFolders("Inbox")
.messages(mail.id)
.attachments()
.buildRequest();
List<Attachment> attachmentsData = attachmentsPage.get().getCurrentPage();
List<AttachmentData> attachmentDataToStore = new java.util.ArrayList<AttachmentData>();
for(Attachment attachment : attachmentsData)
{
attachmentData.setInputStream(
new ByteArrayInputStream(
Base64.getDecoder().decode(attachment.getRawObject()
.get("contentBytes")
.getAsString());
}
從 microsoft-graph 版本 5.34.0 開始,不再需要一些東西,例如解碼,您將能夠簡單地轉換您的 object。
獲取 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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.