I've racked my brain (and others) trying to get this to work. I am pulling a photo via MS Graph API - this part works fine. I am able to receive the data (as bytes). However, I can't get the image to convert properly to be attached as a file and posted.
I have read several postings on SO and GH as well as having tried ~10 different npm packages and flavors(btoa, atob, etc...out of desperation), including the JS example from the Graph docs. No solution has worked. The npm packages all produce different outputs from one another and none of them match the output when I take the photo and upload to an online base64 converter. In addition, if I take the online conversion and place the output string directly into the code, it works.
Here is the current iteration of my code. Any help would be appreciated.
var optionsPhoto = {
url: "https://graph.microsoft.com/v1.0/me/photo/$value",
method: "GET",
headers: {
Authorization: "Bearer " + token
}
};
await request(optionsPhoto, function callback(error, response, body) {
if (!error && response.statusCode == 200) {
photoResponse.data = [
{
"@odata.type": "#microsoft.graph.fileAttachment",
contentBytes: body.split(",").toString("base64"),
contentLocation: "https://graph.microsoft.com/v1.0/me/photo/$value",
isinline: true,
Name: "mypic.jpg"
}
];
photoResponse.ContentType = response.headers["content-type"].toString();
photoResponse.Base64string = (
"data:" +
photoResponse.ContentType +
";base64," +
photoResponse.data[0].contentBytes
).toString();
} else {
console.log(error);
}
});
The .sendActivity
command takes the attachment only as seen below:
await dc.context.sendActivity({
attachments: [
{ contentType: photoResponse.ContentType, contentUrl: photoResponse.Base64string }
]
});