按照给出的问题的说明进行操作后
如何从节点脚本获取 Microsoft Graph API 访问令牌?
为此,我正在使用带有 node/express 的 React,并且我正在学习来自的教程
https://jscomplete.com/learn/1rd-reactful
我的代码/server.js 如下,
import express from 'express';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import App from '../components/App';
const server = express();
server.use(express.static('dist'));
server.get('/', (req, res) => {
const initialMarkup = ReactDOMServer.renderToString(<App />);
res.send(`
<html>
<head>
<title>Sample React App</title>
</head>
<body>
<div id="mountNode">${initialMarkup}</div>
<script src="/main.js"></script>
</body>
</html>
`)
});
const request = require("request");
const endpoint = "https://login.microsoftonline.com/[My Tenant].onmicrosoft.com/oauth2/token";
const requestParams = {
grant_type: "client_credentials",
client_id: "[My ApplicationID]",
client_secret: "[My Secret]",
resource: "https://graph.windows.net"
};
request.post({ url:endpoint, form: requestParams }, function (err, response, body) {
if (err) {
console.log("error");
}
else {
console.log("Body=" + body);
let parsedBody = JSON.parse(body);
if (parsedBody.error_description) {
console.log("Error=" + parsedBody.error_description);
}
else {
console.log("Access Token=" + parsedBody.access_token);
testGraphAPI(parsedBody.access_token);
}
}
});
function testGraphAPI(accessToken) {
request.get({
url:"https://graph.microsoft.com/v1.0/users",
headers: {
"Authorization": "Bearer " + accessToken
}
}, function(err, response, body) {
console.log(body);
})
}
server.listen(4242, () => console.log('Server is running...'));
我得到了访问令牌,但是当我运行函数 testGraphAPI(accessToken) 来测试它时出现错误。它说
{
"error": {
"code": "InvalidAuthenticationToken",
"message": "Access token validation failure. Invalid audience.",
"innerError": {
"request-id": "4abe8022-ebf8-4ae7-b9be-8dd01a460eeb",
"date": "2019-09-28T08:21:07"
}
}
}
我还将我的重定向 url 保存在活动目录中为http://localhost:4242/
有人可以给我有关如何解决此问题的建议,因为我需要使用访问令牌进行进一步的工作。