简体   繁体   English

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

[英]How to get user's photo(profile) using Microsoft Graph API?

When a GET( https://graph.microsoft.com/v1.0/users/ {{user_id}} /photo/$value) request is made, the response data will be written with the same characters as image当 GET( https://graph.microsoft.com/v1.0/users/ {{user_id}} /photo/$value) 请求时,响应数据将写入与图像相同的字符

图片 . .

After converting to base64, I tried blob format but the picture does not appear.转换成base64后,我试了blob格式,但是图片没有出现。

router.js路由器.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图.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索引.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 );
});

Edit: new Buffer is deprected. 编辑:不建议使用新的缓冲区。 Please use Buffer.from(response.data, 'binary').toString('base64'); 请使用Buffer.from(response.data,'binary')。toString('base64');

it works for me 这个对我有用

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');

Finally found a solution to this.终于找到了解决这个问题的方法。 The previous answers didn't quite work for me: What I found that worked was:以前的答案对我来说不太适用:我发现有用的是:

const { data: photoValue} = await axios.request({
          method: 'GET',
          { headers: { Authorization: `Bearer ${token}` },
          responseType: 'blob',
          url: 'https://graph.microsoft.com/v1.0/me/photo/$value',
        });

const blobUrl = window.URL.createObjectURL(photoValue);

and then displaying it with <img src={blobUrl} /> .然后用<img src={blobUrl} />显示它。 This is the shortest answer in my opinion, and relies on the responseType: 'blob' being given.在我看来,这是最短的答案,并且依赖于给出的responseType: 'blob'

I solved this problem. 我解决了这个问题。

router.js 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 index.js

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

'graph.js' is not needed. 不需要“ graph.js”。

reference : Node.js get image from web and encode with base64 参考: Node.js从Web获取图像并使用base64进行编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Microsoft graph API 访问用户的个人资料照片并显示 React 应用程序? - How to access user's profile photo from Microsoft graph API and display React application? 如何使用Linkedin API获取用户的LinkedIn个人资料网址 - How Can I Get User's LinkedIn Profile Url using Linkedin API 如何在wso2 api管理器中获取用户的个人资料? - How to get user's profile in wso2 api manager? 如何使用 Microsoft graph API 获取响应标头 - How to get a response headers using Microsoft graph API 如何使用Ajax调用获取Microsoft Graph API Access令牌 - How to get Microsoft Graph API Access token using ajax call 使用Javascript SDK Graph API获取用户的新闻提要 - Get User's News Feed Using Javascript SDK Graph API 使用Facebook Graph API获取用户朋友的列表 - Using the Facebook Graph API to get a list of a user's friends 如何使用Jquery从JSON API获取用户个人资料信息? - How to get user profile Information from JSON API using Jquery? 如何使用图形API从用户的Facebook新闻源中仅获取照片? - How to get only photos from user's Facebook newsfeed using graph api? Microsoft Graph - 无法显示用户配置文件照片 - Microsoft Graph - Unable to Display User Profile Photos
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM