Microsoft graph api HTTP请求无法使用Microsoft Graph API获取联系人照片
如果我使用library@microsoft/microsoft graph client,我可以将联系人照片作为二进制数据获取,将其转换为base64,并使用以下代码获取正确的照片:Microsoft graph api HTTP请求无法使用Microsoft Graph API获取联系人照片,microsoft-graph-api,Microsoft Graph Api,如果我使用library@microsoft/microsoft graph client,我可以将联系人照片作为二进制数据获取,将其转换为base64,并使用以下代码获取正确的照片: const request = require('request') const microsoftGraph = require('@microsoft/microsoft-graph-client'); let token = token-value let client =
const request = require('request')
const microsoftGraph = require('@microsoft/microsoft-graph-client');
let token = token-value
let client = getMicrosoftGrapClient(token);
let id = contact-id;
let url = '/me/contacts/' + id + '/photo/$value';
client.api(url).get().then((res) => {
//console.log(res);
var encodedImage = new Buffer(res, 'binary').toString('base64');
console.log("encodedImage>>>>>>>>>>>>>>>>>>>>>>")
console.log (encodedImage);
}).catch((err) => {
console.log(err);
});;
function getMicrosoftGrapClient (token) {
// Create a Graph client
return microsoftGraph.Client.init({
authProvider: (done) => {
// Just return the token
done(null, token);
}});
}
我无法使用HTTP get获取正确的联系人照片。HTTP
响应代码为200,但主体不是photo的二进制数据。
请告诉我错误是什么。代码如下:
const request = require('request')
request({
url: "https://graph.microsoft.com/v1.0/me/contacts/{contact_id}/photo/$value",
method: 'GET',
headers: {
'content-type': 'image/jpg',
'Authorization': 'Bearer {token}'
}
}, function (error, response, body){
console.log(error);
var encodedImage = new Buffer(body, 'binary').toString('base64');
console.log(encodedImage);
});
编码需要明确指定为
encoding: null
在这种情况下,主体将是缓冲区类型
,而不是默认的(字符串
)
而内容类型
可以省略
范例
request({
url: "https://graph.microsoft.com/v1.0/me/photo/$value",
method: 'GET',
encoding: null,
headers: {
'Authorization': 'Bearer ' + accessToken,
}
}, function (error, response, body) {
var encImage = new Buffer(body, 'binary');
fs.writeFileSync(filePath, encImage );
});