我正在构建一个页面,其中对不同端点多次调用 Microsoft Graph:获取 OneDrive 文件、电子邮件、用户属性等。
唯一一个不起作用的调用是获取当前用户的日历事件。我使用的端点是 https://graph.microsoft.com/v1.0/me/events
。响应是 403 Forbidden。
根据微软文档here应用程序需要 Calendars.Read
或 Calendars.ReadWrite
权限。我在委派权限下检查了这两个内容,但仍然存在同样的问题。然后我在 Azure AD 中为此应用勾选了所有 51 个权限范围,但仍然存在同样的问题。
我还尝试在 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/