【问题标题】:Microsoft Graph API Unauthorized Error 401Microsoft Graph API 未经授权的错误 401
【发布时间】:2021-03-17 00:05:21
【问题描述】:

我是 MS 图形 API 的新手。我正在学习这个 API,我遵循了这个教程https://docs.microsoft.com/en-us/azure/active-directory/develop/tutorial-v2-nodejs-console,它在检索用户时对我来说很好。这是代码

async function main() {
    try {
        // here we get an access token
        const authResponse = await auth.getToken(auth.tokenRequest);
        console.log("get auth reespones ", authResponse)

        const options = {
            method: 'get',
            headers: {
                Authorization: `Bearer ${authResponse}`
            }
        };
        // call the web API with the access token
        const users = await fetch.callApi(auth.apiConfig.uri, options);

        console.log("get users ", users)
    } catch (error) {
        console.log("error here",error);
    }
};

但我正在尝试调用其他 API,并且 我在访问日历 API 时遇到问题。 这是我用来在 ms graph 中调用日历 api 的新函数。

async function getcalendar() {
    try {
        // here we get an access token
        const authResponse = await auth.getToken(auth.tokenRequest);

        const options = {
            method: 'get',
            headers: {
                Authorization: `Bearer ${authResponse}`,
                Prefer: `outlook.timezone="Pacific Standard Time"`
            }
        }
        // call the web API with the access token
        const users = await fetch.callApi('https://graph.microsoft.com/v1.0/me/calendar', options);

        console.log("get users ", users)
    } catch (error) {
        console.log("error is here ",error);
    }
};

在我的天蓝色应用程序中,我已经设置了所有权限

我不知道为什么它一直说未经授权。

感谢任何帮助。谢谢

【问题讨论】:

  • 您使用的是个人帐号吗?
  • @ShivaKeshavVarma 是的,我是 ..
  • 您好,如果发布的答案解决了您的问题,请单击复选标记将其标记为答案。这样做可以帮助其他人找到他们问题的答案。见meta.stackexchange.com/questions/5234/…
  • 如何设置请求正文?

标签: microsoft-graph-api azure-ad-graph-api


【解决方案1】:

尝试将Calendars.Read、Calendars.ReadWrite 设置为Delegated 权限类型而不是Application 类型。

getSchedule api 不支持个人微软账号。

【讨论】:

  • 我已经改了,但是还是一样的错误..
【解决方案2】:

您不能使用个人帐户点击me/calendar/getschedule,因为这不受支持。

permissions 仅适用于工作或学校帐户或应用令牌。

当您使用应用程序上下文时,您需要进行如下调用。

/users/{id|userPrincipalName}/calendar/getSchedule 因为如果没有用户参与,me 就没有意义。

【讨论】:

  • 你好,我不使用delegated,我使用application,但使用个人帐户。
  • 现在我知道了@theboyonfire,使用像/users/{id|userPrincipalName}/calendar/getSchedule 这样的http 调用。权限中的Application 选项只不过是应用程序上下文,这意味着没有用户参与。在这里你不能使用me,因为没有用户:)-
  • 您好,谢谢您的回复。但是我现在在使用 post 方法传递 graph.microsoft.com/v1.0/users/… 时收到错误 404 :(
  • 您使用的是工作帐户还是学校帐户的用户 ID?
猜你喜欢
  • 1970-01-01
  • 2017-05-05
  • 1970-01-01
  • 2019-02-01
  • 2019-05-06
  • 2018-09-23
  • 1970-01-01
  • 2016-02-09
  • 1970-01-01
相关资源
最近更新 更多