seung7642
seung7642

Reputation: 91

How do I get the raw content of attachment resources from Java SDK in MS Graph API?

I want to use the Java SDK of MS Graph to look up raw content (/$value endpoint) of the attachment.

For /messages resources, I know that raw content can be inquired in the following way.

@Test
void downloadEmlFile() {
    // while empty value, but let's assume there is a value.
    String userId = ""; 
    String messageId = "";

    InputStream inputStream = 
        graphServiceClient.users(userId).messages(messageId)
            .content()
            .buildRequest()
            .get();
}

As you can see above, MS Graph SDK provides an api called .content() for messages.

However, it seems that they do not provide the corresponding api for the attachment resource.

In summary, there are two things I am curious about

  1. Why don't you provide an API like .content() for the attachment resource?
  2. How do I get raw content (/$value) for an attachment?

Upvotes: 2

Views: 954

Answers (2)

mehmettasar
mehmettasar

Reputation: 1

You can use the FileAttachment class.

FileAttachment attachment = (FileAttachment) message.attachments.getCurrentPage().get(0);
attachment.contentBytes

Upvotes: 0

user2250152
user2250152

Reputation: 20768

I think you need to use the FileAttachmentRequestBuilder which has the content method.

@Test
void downloadEmlFile() {
    // while empty value, but let's assume there is a value.
    String userId = ""; 
    String messageId = "";
    String attachmentId = "";

    String requestUrl = graphServiceClient.users(userId).messages(messageId)
            .attachments(attachmentId)
            .getRequestUrl();

    FileAttachmentRequestBuilder fileAttachReqBuilder = new FileAttachmentRequestBuilder(
             requestUrl, 
             graphServiceClient, 
             null)

    InputStream inputStream = fileAttachReqBuilder
                                 .content()
                                 .buildRequest()
                                 .get();
}

Upvotes: 2

Related Questions