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

How to get user#39;s photo(profile) using Microsoft Graph API?(如何使用 Microsoft Graph API 获取用户的照片(个人资料)?)

本文介绍了如何使用 Microsoft Graph API 获取用户的照片(个人资料)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个 GET(.

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

router.js

router.get('/photo/:id',function (req,res) {auth.getAccessToken().then(function (token){让 userId = req.params.id;graph.getUserPhotoData(token, userId).then(function (result) {res.json(结果);}).catch(函数 (e) { console.log(e) })});});

graph.js

function getUserPhoto(token, userId){返回 axios({方法:'get',网址:'https://graph.microsoft.com/v1.0/users/'+{{user_id}}+'/photo/$value',标题:{'授权':令牌,//'内容类型': '图片/jpeg',},响应类型:'blob'})}异步函数 getUserPhotoData(token,userId) {尝试{让 userPhoto = getUserPhoto(token,userId);让 p = userPhoto.data;//让 photo = new Buffer(userPhoto.data).toString('base64');返回 p;//...013O✿u0011 e    |  > 4+ y  u0017 "Y...}catch (e) { console.log(e);}}

index.js

$.get('/photo/'+userId, function(response) {让二进制数据 = [];binaryData.push(响应);const blobUrl = window.URL.createObjectURL(new Blob(binaryData, {type: "image/jpeg"}));document.getElementById('user-img').setAttribute("src", blobUrl );});

解决方案

新缓冲区已被弃用.请使用 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');

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

.

After converting to base64, I tried blob format but the picture does not appear.

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

解决方案

Edit: new Buffer is deprected. Please use 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');

这篇关于如何使用 Microsoft Graph API 获取用户的照片(个人资料)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用 Microsoft Graph API 获取用户的照片(个人资料)?

基础教程推荐