使用 Microsoft Graph API 获取 SharePoint Online 团队网站

Using Microsoft Graph API to get SharePoint Online team sites

我正在尝试访问组织的 SharePoint 团队网站。我正在使用 Microsoft Graph API,因为它是最完整的 Office 365 API。我了解如何获取访问令牌以及如何使用它发出请求。我知道它有效,因为我可以获得组列表,但是在获取所有团队站点时,我收到一条错误消息:

Code : invalidRequest    
Message : Cannot enumerate sites

这是我的代码。我正在控制台应用程序中对其进行测试。

class Program {
    static void Main(string[] args) {
        Program p = new Program();
        var items = p.GetItems();
        items.Wait();
        foreach (var item in items.Result) {
            Console.WriteLine(item.DisplayName);
        }

        Console.ReadLine();
    }

    public async Task<IGraphServiceSitesCollectionPage> GetItems() {
        PublicClientApplication myApp =
            new PublicClientApplication("CLIENT-ID-FROM-DEVELOPPER-CONSOLE");

        //Gets an access token
        AuthenticationResult authenticationResult =
            await myApp.AcquireTokenAsync(
                new string[] {
                    "user.read.all",
                    "sites.read.all",
                    "files.read.all",
                    "group.read.all",
                    "Directory.Read.All"
                }).ConfigureAwait(false);

        //Creates the client with the access token
        GraphServiceClient graphClient = new GraphServiceClient(
            new DelegateAuthenticationProvider(
                async(requestMessage) => {
                    // Append the access token to the request.
                    requestMessage.Headers.Authorization =
                        new AuthenticationHeaderValue("bearer",
                            authenticationResult.AccessToken);
                }));

        //Requests the site objects
        var sites = await graphClient.Sites.Request().GetAsync();

        return sites;
    }
}

我用 Google 搜索了很多,只找到了对我不起作用的解决方案。

错误消息准确描述了为什么这不起作用。

graphClient.Sites.Request().GetAsync() 的调用被转换为 HTTP 调用 https://graph.microsoft.com/sites,这不是有效的 API 端点。

您需要提供一些额外的背景信息,例如您正在寻找的 site。例如,要获取根站点,您可以调用:

graphClient.Sites["root"].Request().GetAsync();

如果您正在寻找团队网站,您可以使用该网站的路径:

await graphClient.Sites["root"].SiteWithPath("/teams/myawesometeam").Request().GetAsync();

有关其他 SharePoint 端点,请参阅 Graph SharePoint documentation