node.js - 使用请求的 Microsoft-Graph token 太小

标签 node.js microsoft-graph-api

我正在尝试使用请求发送 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/

相关文章:

node.js - 如何检查变量是否为NodeJS中的矩对象

c# - 未找到方法 'Android.Support.CustomTabs.CustomTabsIntent.LaunchUrl'

jquery - 使用jquery ajax调用Microsoft Graph API和officeLocation返回null

microsoft-graph-api - 无需授权完全读取权限即可获取忙/闲信息

javascript - 在 Jest 测试中使用expect.assertions(x) 与使用fail()

node.js - Sails JS Passport session 持久性问题 : Mocha Test Failing

office365 - 通过API获取电子邮件别名

excel - 访问onedrive的工作簿时找不到Microsoft Graph API资源

node.js - 尝试构建 "Error: self signed certificate in certificate chain"时堆栈 'node-gyp configure'

node.js - NodeJS SHA1 获取原始输出(PHP SHA1 原始输出等效)