How to get raw contents of attachment using Graph API?

Viewed 150

I'm using the Microsoft Graph API to get the contents of a file, but something is off about the encoding with the content bytes being returned. When I view the file in Notepad++, the contents look something like this:

UÏMÌØˆ€aX!D"AŠMtPïq¡ª†f24O]J¦¢’k¿ÇFcX+|¿¯÷q”z{CîËC~ãZVŸâÆù ´*ëáµå:ã/•þ57Yü*—kØ&Ž=NDüßÒLó>@÷.MÊ
×núšÙÄlsb´Ò·SFAÙá~˜ wpÌ&—wY¯ÇpÊúé(¯uïû°×Œ†ˆ÷.”ÒB1ŒKŽ|G;m««¼Õ‘h’IRLp°,µ‰<ÿ¥*‡ë>Ž´ÌÇRtA‰öjJäÂY<5&u„óNæÓÓ¿Md¡U§ÎkäÐÁJÛO{9’^¬C]Ó<ô;Ë:N“šK8KÏH1

I call the Graph endpoint to get the contents of the file like this:

public function getRawAttachmentContents($emailAddress, $messageId)
{
    return $this->graph->createRequest("GET", "/users/$emailAddress/messages/$messageId/attachments/{attachmentId}")
        ->setReturnType(get_class(new \Microsoft\Graph\Model\FileAttachment()))
        ->execute();
}

This is what gets returned when I make the call. The contents look very different. It's like they're in a different format. How can I get them to be in the same format as the original? When I call the Graph API to get the raw contents (using the $value at the end) nothing gets returned. enter image description here

Edit: So I figured out how to get the contents of the attachment as they appear when I open the file in Notepad++. I do this:

public function getRawAttachmentContents($emailAddress, $messageId, $attachmentId) { $attachment = $this->graph->createRequest("GET", "/users/$emailAddress/messages/$messageId/attachments/$attachmentId/$value") ->execute(); $attachment->getRawBody()->rewind(); $file_contents = $attachment->getRawBody()->getContents(); return $file_contents; }

However, when I try uploading the contents returned from that function, an attachment doesn't even get created in the draft message (before, when I uploaded the plain text, it was at least creating an attachment to the draft message, and I was getting a 201 created response on the final upload of the bytes, but the attachment was corrupt).

0 Answers
Related