Javascript 如何获得用户';使用Microsoft Graph API的照片(配置文件)?
当发出GET({user_id}}/photo/$value)请求时,响应数据将使用与image相同的字符写入 转换到base64后,我尝试了blob格式,但图片没有出现 router.jsJavascript 如何获得用户';使用Microsoft Graph API的照片(配置文件)?,javascript,microsoft-graph-api,Javascript,Microsoft Graph Api,当发出GET({user_id}}/photo/$value)请求时,响应数据将使用与image相同的字符写入 转换到base64后,我尝试了blob格式,但图片没有出现 router.js router.get('/photo/:id',function (req,res) { auth.getAccessToken().then(function (token){ let userId = req.params.id; graph.getUserPhotoData(toke
router.get('/photo/:id',function (req,res) {
auth.getAccessToken().then(function (token){
let userId = req.params.id;
graph.getUserPhotoData(token, userId).then(function (result) {
res.json(result);
}).catch(function (e) { console.log(e) })
});
});
function getUserPhoto(token, userId){
return axios({
method : 'get',
url : 'https://graph.microsoft.com/v1.0/users/'+{{user_id}}+'/photo/$value',
headers: {
'Authorization':token,
// 'Content-Type': 'image/jpeg',
},
responseType : 'blob'
})
}
async function getUserPhotoData(token,userId) {
try{
let userPhoto = getUserPhoto(token,userId);
let p = userPhoto.data;
// let photo = new Buffer(userPhoto.data).toString('base64');
return p; //...013O✿\u0011�e����|��>�4+�y��\u0017�"Y...
}catch (e) { console.log(e);}
}
$.get('/photo/'+userId, function(response) {
let binaryData = [];
binaryData.push(response);
const blobUrl = window.URL.createObjectURL(new Blob(binaryData, {type: "image/jpeg"}));
document.getElementById('user-img').setAttribute("src", blobUrl );
});
graph.js
router.get('/photo/:id',function (req,res) {
auth.getAccessToken().then(function (token){
let userId = req.params.id;
graph.getUserPhotoData(token, userId).then(function (result) {
res.json(result);
}).catch(function (e) { console.log(e) })
});
});
function getUserPhoto(token, userId){
return axios({
method : 'get',
url : 'https://graph.microsoft.com/v1.0/users/'+{{user_id}}+'/photo/$value',
headers: {
'Authorization':token,
// 'Content-Type': 'image/jpeg',
},
responseType : 'blob'
})
}
async function getUserPhotoData(token,userId) {
try{
let userPhoto = getUserPhoto(token,userId);
let p = userPhoto.data;
// let photo = new Buffer(userPhoto.data).toString('base64');
return p; //...013O✿\u0011�e����|��>�4+�y��\u0017�"Y...
}catch (e) { console.log(e);}
}
$.get('/photo/'+userId, function(response) {
let binaryData = [];
binaryData.push(response);
const blobUrl = window.URL.createObjectURL(new Blob(binaryData, {type: "image/jpeg"}));
document.getElementById('user-img').setAttribute("src", blobUrl );
});
index.js
router.get('/photo/:id',function (req,res) {
auth.getAccessToken().then(function (token){
let userId = req.params.id;
graph.getUserPhotoData(token, userId).then(function (result) {
res.json(result);
}).catch(function (e) { console.log(e) })
});
});
function getUserPhoto(token, userId){
return axios({
method : 'get',
url : 'https://graph.microsoft.com/v1.0/users/'+{{user_id}}+'/photo/$value',
headers: {
'Authorization':token,
// 'Content-Type': 'image/jpeg',
},
responseType : 'blob'
})
}
async function getUserPhotoData(token,userId) {
try{
let userPhoto = getUserPhoto(token,userId);
let p = userPhoto.data;
// let photo = new Buffer(userPhoto.data).toString('base64');
return p; //...013O✿\u0011�e����|��>�4+�y��\u0017�"Y...
}catch (e) { console.log(e);}
}
$.get('/photo/'+userId, function(response) {
let binaryData = [];
binaryData.push(response);
const blobUrl = window.URL.createObjectURL(new Blob(binaryData, {type: "image/jpeg"}));
document.getElementById('user-img').setAttribute("src", blobUrl );
});
我解决了这个问题
router.js
const request=require('request');
router.get('/photo/:id',函数(req,res){
auth.getAccessToken().then(函数(令牌){
让userId=req.params.id;
//graph.getUserPhotoData(令牌,用户ID).then(函数(结果){
//控制台日志(结果);
//res.json(结果);
//}).catch(函数(e){console.log(e)})
请求({uri:'https://graph.microsoft.com/beta/users/“+userId+”/photo/$value',方法:“GET”,标题:{'Authorization':'Bearer'+token},编码:null},
功能(错误、响应、正文){
let data=“data:”+response.headers[“content type”]+“base64”+新缓冲区(body).toString('base64');
res.send(data);//数据:image/jpeg;base64,/9j/4AAQSkZJRg。。。
});
});
});代码>编辑:不推荐使用新缓冲区。请使用Buffer.from(response.data,'binary').toString('base64')
它对我有用
const graphEndpoint = "https://graph.microsoft.com/v1.0/me/photo/$value";
const response = await axios(graphEndpoint, { headers: { Authorization: `Bearer ${token}` }, responseType: 'arraybuffer' });
const avatar = new Buffer(response.data, 'binary').toString('base64');
谢谢很长一段时间都在挣扎。为了完整,你可以像这样使用化身this.user.photo='数据:image/jpeg;base64,“+avatar
不再适用于我。微软同时改变了吗?