首页 > 解决方案 > 在 SPfx 应用程序中获取用户事件的 Microsoft GraphAPI 403 错误

问题描述

我使用 Microsoft GraphAPI 编写了一个托管在 SharePoint Online 上的 SharePoint SPfx 应用程序,以获取用户配置文件信息和日历事件。根据微软文档,我在 package-solution.json 文件中声明了我的范围,并通过 SharePoint APi 管理页面批准了请求。我可以读取每个人的个人资料信息,但是,当我尝试访问日历事件但我自己的事件时,我收到错误 403。用户的日历不是私人的,它们对整个组织开放。

Package-solution.json 权限请求

"webApiPermissionRequests": [
      {
        "resource": "Microsoft Graph",
        "scope": "User.Read"
      },
      {
        "resource": "Microsoft Graph",
        "scope": "User.ReadBasic.All"
      },
      {
        "resource": "Microsoft Graph",
        "scope": "People.Read"
      },
      {
        "resource": "Microsoft Graph",
        "scope": "Calendars.Read"
      }
    ]
  }

管理员批准的请求图片

已批准的应用权限屏幕

请求代码:

private _searchUserCalendar(keyword: string): Promise<any[]> {
    console.log("connection to GraphAPI event domain")
    return new Promise<any[]>((resolve, reject) => {
      this._context.msGraphClientFactory.getClient()
        .then((client: MSGraphClient): void => {
          client
            .api(`/users/${keyword}/calendar/events`) // The api i.e> /me | /users
            .version('v1.0')
            .select("showAs,start,subject, end")
            .top(5)
            .get((error, response: any, rawResponse?: any) => {
              if (error) {
                console.log("ooops somethign went wrong get events",error);
                reject(error);
              }
              var users:Array<any>=new Array<any>();

              // Map the JSON response to the output array
              if (response != null && response != undefined) {
                console.log(response);
                response.value.map((item: any) => {
                  console.log("found events for:", item)
                }); // mapping over users
              }
              resolve(users);  

            });
        });
    });
  }

错误响应

{error: {code: "ErrorAccessDenied", message: "Access is denied. Check credentials and try again.",…}}
error: {code: "ErrorAccessDenied", message: "Access is denied. Check credentials and try again.",…}
code: "ErrorAccessDenied"
message: "Access is denied. Check credentials and try again."
innerError: {request-id: "5dca3d4f-ab4c-4237-8ff4-78d8cacbd43b", date: "2020-01-16T19:05:26"}
request-id: "5dca3d4f-ab4c-4237-8ff4-78d8cacbd43b"
date: "2020-01-16T19:05:26"

我已经尝试了一切,任何帮助将不胜感激。

标签: sharepointmicrosoft-graph-apispfx

解决方案


User.Read,并且User.ReadBasic.All无权执行您正在尝试执行的操作。

URL讨论日历阅读共享事件。

例如,Garth 与 John 共享了他的默认日历并授予 John 读取权限。如果 John 已登录您的应用并提供了委派权限(Calendars.Read.Shared 或 Calendars.ReadWrite.Shared),您的应用将能够访问 Garth 的默认日历和该日历中的事件,如下所述。

你可以尝试添加Calendar.Read.Shared


推荐阅读