我正在尝试使用请求发送 POST 请求来获取 Microsoft-Graph 应用程序的访问 token 。 问题是我得到的响应 token 只有 1188 个字符长。虽然我使用 Postman 时获得的 token 长度为 1461 个字符。每次我发送一个请求,它都会生成一个全新的 token ,但是我在nodejs中通过请求得到的总是1188,而在postman中得到的总是1461。
我尝试了不同的方法,例如在 Microsoft Azure 中生成新的应用程序 api id,但它始终给出相同的结果。
这是我的代码,我删除了敏感信息,但将其替换为“审查”一词。 我在 Postman 中使用完全相同的请求参数
const endpoint = "https://login.microsoftonline.com/CENSORED/oauth2/token";
const requestParams = {
client_id: "CENSORED",
client_secret: "CENSORED",
resource: "https://graph.windows.net",
grant_type: "client_credentials"
};
let accessToken = await
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 {
getCalendarEvents(parsedBody.access_token);
}
}
});
Postman 返回 1461 个字符长的访问 token ,而 Nodejs 请求仅返回 1188 个字符长的访问 token 。
这就是我在 Nodejs 中使用的:https://www.npmjs.com/package/request
最佳答案
资源https://graph.windows.net
适用于旧版Azure AD Graph API 。这是一个与 the Microsoft Graph 完全独立的 API 。对于 Microsoft Graph,资源
应为 https://graph.microsoft.com
:
const endpoint = "https://login.microsoftonline.com/{tenant}/oauth2/token";
const requestParams = {
client_id: "{clientid}",
client_secret: "{clientsecret}",
resource: "https://graph.microsoft.com",
grant_type: "client_credentials"
};
关于node.js - 使用请求的 Microsoft-Graph token 太小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56321709/