How to get user#39;s photo(profile) using 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 获取用户的照片(个人资料)?
基础教程推荐
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01