javascript - Microsoft Graph API - v1.0/me/events 禁止 403

标签 javascript jquery calendar office365 microsoft-graph-api

我正在构建一个页面,其中对不同端点多次调用 Microsoft Graph:获取 OneDrive 文件、电子邮件、用户属性等。

唯一一个不起作用的调用是获取当前用户的日历事件。我使用的端点是 https://graph.microsoft.com/v1.0/me/events。响应是 403 Forbidden。

enter image description here

根据微软文档here应用程序需要 Calendars.ReadCalendars.ReadWrite 权限。我在委派权限下检查了这两个内容,但仍然存在同样的问题。然后我在 Azure AD 中为此应用勾选了所有 51 个权限范围,但仍然存在同样的问题。

enter image description here

我还尝试在 Azure AD 中创建新应用,但这没有帮助。

如何使用 Microsoft Graph 取回当前用户的日历事件?我错过了什么?

编辑:

我使用 ADAL.js 进行身份验证。这是我自己的 doAuth 函数中的代码,它接收应用程序的客户端 ID。

function doAuth(clientId) {
    var variables = {
        // Domain of Azure AD tenant
        azureAD: // the appropriate URL,
        // ClientId of Azure AD application principal
        clientId: clientId,
        // Name of SharePoint tenant
        sharePointTenant: // the appropriate URL
    }

    // Create config and get AuthenticationContext
    window.config = {
        tenant: variables.azureAD,
        clientId: variables.clientId,
        postLogoutRedirectUri: window.location.origin,
        endpoints: {
            graphApiUri: "https://graph.microsoft.com",
            sharePointUri: "https://" + variables.sharePointTenant + ".sharepoint.com",
        },
        cacheLocation: "localStorage"
    }

    var authContext = new AuthenticationContext(config);
    var isCallback = authContext.isCallback(window.location.hash);
    authContext.handleWindowCallback();

    if (isCallback && !authContext.getLoginError()) {

        window.location = authContext._getItem(authContext.CONSTANTS.STORAGE.LOGIN_REQUEST);
    }

    var user = authContext.getCachedUser();
    var token = authContext.getCachedToken(clientId);

    if (!user || !token)
        authContext.login();

    return authContext
}

最佳答案

听起来您已经更改了分配给应用程序的范围。发生这种情况时,您还需要让用户使用这些新范围重新授权。为此,请将 &prompt=consent 添加到初始 ODATA 重定向的查询字符串。这将强制将您的新范围呈现给用户以进行授权。

您可以使用配置中的 extraQueryParameter 参数在 ADAL.js 库中触发此操作:

// Create config and get AuthenticationContext
window.config = {
    tenant: variables.azureAD,
    clientId: variables.clientId,
    postLogoutRedirectUri: window.location.origin,
    endpoints: {
        graphApiUri: "https://graph.microsoft.com",
        sharePointUri: "https://" + variables.sharePointTenant + ".sharepoint.com",
    },
    cacheLocation: "localStorage",
    extraQueryParameter: "prompt=consent"
}

关于javascript - Microsoft Graph API - v1.0/me/events 禁止 403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42067217/

相关文章:

javaScript 提升无法正常工作

javascript - 从 Promise 返回一个值

Javascript/JQuery 隐藏和显示

javascript - 显示值的 knockout 文本绑定(bind)

javascript - 在 FullCalendar 问题中设置默认点击选择突出显示

android - 日历中 rrule 的日期格式

javascript - 如何在 d3 文本函数中返回 html?

javascript - 如何使用 JQuery 获取 HTML 元素数组中的 HTML 元素

javascript - 数据表插件无法自定义动态加载的表

iOS设计问题: How should switching between days be implemented in a table view?