【问题标题】:MS Graph API rejects tokenMS Graph API 拒绝令牌
【发布时间】:2019-02-20 10:12:13
【问题描述】:

我正在尝试创建一个 node.js 服务来访问我用户的日历并通过 MS Graph API 向其他人发送邀请。

因此,我遵循了本指南: https://docs.microsoft.com/en-us/graph/auth-v2-service

const endpoint = "https://login.microsoftonline.com/[tenant]/oauth2/token";
const requestParams = {
    grant_type: "client_credentials",
    client_id: "[appid]",
    scope: 'https://graph.microsoft.com/.default',
    client_secret: "[password/secret]"
};

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);
            requestData(parsedBody.access_token)
        }
    }
});

function requestData(accessToken) {
    request.get({
        url: "https://graph.microsoft.com/v1.0/me/calendars",
        headers: {
          "Authorization": "Bearer " + accessToken
        }
    }, function(err, response, body) {
        console.log(body);
    });
}

我可以成功检索访问令牌,但是当我尝试请求任何 Graph API 时收到错误消息:

{
  "error": {
    "code": "InvalidAuthenticationToken",
    "message": "Access token validation failure.",
    "innerError": {
      "request-id": "..",
      "date": "2019-02-17T09:44:09"
    }
  }
}

任何提示我可能做错了什么?

【问题讨论】:

    标签: microsoft-graph-api adal msal


    【解决方案1】:

    您可能会收到此错误,因为您在代码中尝试通过将令牌请求发送到 /[tenant]/oauth2/token 端点来获取访问令牌。根据您遵循的文档,您需要将访问请求发送到 V2 端点 /[tenant]/oauth2/v2.0/token

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多