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 获取用户的照片(个人资料)?


基础教程推荐
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01