当你要照片的时候
/$value
request
然而,客户将尸体视为
utf8
默认情况下基于字符串。
为了重新训练原始二进制值,需要显式地告诉
请求
你不想发生这种事。这是通过设置
encoding: null
encoding
-要在上使用的编码
setEncoding
null
,的
body
作为
Buffer
. 还有别的吗?
(包括
undefined
将作为
encoding
参数到
toString()
(这意味着
默认情况下)。(
注:
如果需要二进制数据,应该设置
.)
代码如下所示:
var optionsPhoto = {
url: "https://graph.microsoft.com/v1.0/me/photo/$value",
encoding: null, // Tells request this is a binary response
method: "GET",
headers: {
Authorization: "Bearer " + token
}
};
await request(optionsPhoto, function callback(error, response, body) {
if (!error && response.statusCode == 200) {
// Grab the content-type header for the data URI
const contentType = response.headers["content-type"];
// Encode the raw body as a base64 string
const base64Body = body.toString("base64");
// Construct a Data URI for the image
const base64DataUri = "data:" + contentType + ";base64," + base64Body;
// Assign your values to the photoResponse object
photoResponse.data = [
{
"@odata.type": "#microsoft.graph.fileAttachment",
contentBytes: base64Body,
contentLocation: optionsPhoto.url,
isinline: true,
Name: "mypic.jpg"
}
];
photoResponse.ContentType = contentType;
photoResponse.Base64string = base64DataUri;
} else {
console.log(error);
}
});