Microsoft Graph API 未授权错误 401

时间:2021-03-16 09:57:09

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

我是 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 图中调用日历 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);
    }
};

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

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

enter image description here

感谢任何帮助。谢谢

2 个答案:

答案 0 :(得分:0)

尝试为 Calendars.Read 权限类型而不是 Calendars.ReadWrite 类型设置 DelegatedApplication

getSchedule api 不支持个人 Microsoft 帐户。

答案 1 :(得分:0)

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

enter image description here

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

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

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