如何使用 Microsoft Graph API 获取用户的照片(个人资料)?

soj*_*ung 3 javascript microsoft-graph-api

当发出 GET( https://graph.microsoft.com/v1.0/users/ {{user_id}} /photo/$value) 请求时,响应数据将写入与图像相同的字符

图片.

转换为base64后,我尝试了blob格式,但图片没有出现。

路由器.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) })
  });
});
Run Code Online (Sandbox Code Playgroud)

图.js

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);}
}
Run Code Online (Sandbox Code Playgroud)

索引.js

$.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 );
});
Run Code Online (Sandbox Code Playgroud)

Kry*_*iak 8

编辑:新缓冲区已弃用。请使用 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');
Run Code Online (Sandbox Code Playgroud)

  • 为了完整起见,您可以使用像这样的头像 `this.user.photo = 'data:image/jpeg;base64, ' + avatar` (2认同)
  • 不再对我有用了。微软同时改变了吗? (2认同)

小智 0

我解决了这个问题。

路由器.js

const request = require('request');

router.get('/photo/:id',function (req,res) {
  auth.getAccessToken().then(function (token){
    let userId = req.params.id;
    // graph.getUserPhotoData(token, userId).then(function (result) {
    //   console.log(result);
    //   res.json(result);
    // }).catch(function (e) { console.log(e) })
    request({ uri: 'https://graph.microsoft.com/beta/users/'+userId+'/photo/$value', method: "GET", headers:{'Authorization' : 'Bearer' + token}, encoding: null},
    function(error, response, body) {
      let data = "data:" + response.headers["content-type"] + ";base64," + new Buffer(body).toString('base64');
      res.send(data); //data:image/jpeg;base64,/9j/4AAQSkZJRg...
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

索引.js

$.get('/photo/'+ userId, function(response) {
  document.getElementById('user-img').setAttribute("src", response);
});
Run Code Online (Sandbox Code Playgroud)

不需要“graph.js”。

参考: Node.js从网络获取图像并使用base64编码