【发布时间】:2018-06-26 14:11:33
【问题描述】:
我正在处理 nodejs 项目,我需要调用几个 ms 图形 api 来获取一些数据。一些示例调用如下
https://graph.microsoft.com/v1.0/users/{userPrincipalName}/manager
https://graph.microsoft.com/beta/users/{userPrincipalName}/notebooks
https://graph.microsoft.com/v1.0/users/{userPrincipalName}/photo/
我的目标是不要每次都使用请求模块来调用以上 3 个 api 并分别获取数据。我想创建某种辅助函数,我可以在其中传递我需要使用的 URL 以及 {userPrincipalName} 是什么
所以我创建了 graphendpoints.js 来存储所有 url 的
module.exports = {
manager: 'https://graph.microsoft.com/v1.0/users/userPrincipalName/manager',
notebooks: 'https://graph.microsoft.com/beta/users/{userPrincipalName}/notebooks',
photo: 'https://graph.microsoft.com/v1.0/users/{userPrincipalName}/photo/'
};
这就是我通过身份验证令牌和网址通过单个函数调用图形的方式。我没有每个单独的方法来执行对经理、笔记本或照片等的请求。
executeGraphApi: function (token, url) {
return new Promise(function (resolve, reject) {
let parsedBody = []
request.get({
url: url,
headers: {
authorization: 'Bearer ' + token
}
},
function (err, response, body) {
if (err) {
reject(err)
} else {
parsedBody.push(JSON.parse(body).displayName)
resolve(parsedBody)
}
})
});
}
帮助函数runMSGraphApi(url)调用上面的executeGraphApi(token,url)
var graphcallrunner = require('../../app/msgraph/graphcallrunner');
runMSGraphApi: function (url) {
return new Promise(function (resolve,reject) {
msGraph.getAccessToken()
.then(function (token) {
graphcallrunner.executeGraphApi(token,url)
.then(function (results) {
resolve(results)
})
.catch(function (error) {
reject(error)
})
})
.catch(function (error) {
console.log(error);
reject(error)
})
})
}
这个的消费者如下所示。我需要确保想要调用 ms graph 的人只需使用 runMSGraphApi() 并传入正确的 url。
var graphendpoints = require('../../../msgraph/graphendpoints.js');
graphapihelper.runMSGraphApi(graphendpoints.manager),
如果您在上面看到,我从 graphendpoints.js 中选择了 manager api 调用并传入了该 url。这个网址其实是 https://graph.microsoft.com/v1.0/users/{userPrincipalName}/manager
其中 {userPrincipalName} 未注入。如何将 {userPrincipalName} 的值动态传递给 executeGraphApi() 中的最终 url 构造。
https://graph.microsoft.com/v1.0/users/bob.bar@google.com/manager
【问题讨论】:
-
为什么不使用 Microsoft Graph JavaScript SDK? github.com/microsoftgraph/msgraph-sdk-javascript
标签: node.js express microsoft-graph-api node-modules