【问题标题】:How to get user's photo(profile) using Microsoft Graph API?如何使用 Microsoft Graph API 获取用户的照片(个人资料)?
【发布时间】:2018-05-19 04:56:12
【问题描述】:

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

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

router.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) })
  });
});

graph.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);}
}

index.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 );
});

【问题讨论】:

    标签: javascript microsoft-graph-api


    【解决方案1】:

    编辑:新缓冲区已被弃用。请使用 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 = 'data:image/jpeg;base64, ' + avatar
    • 不再适合我了。微软是否同时改变了它?
    【解决方案2】:

    我解决了这个问题。

    router.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...
        });
      });
    });

    index.js

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

    不需要'graph.js'。

    参考: Node.js get image from web and encode with base64

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 2022-06-16
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多