MS Graph API拒绝令牌

时间:2019-02-20 10:12:13

标签: microsoft-graph adal msal

我正在尝试创建一个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"
    }
  }
}

有人暗示我做错了什么吗?

1 个答案:

答案 0 :(得分:0)

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