使用 Microsoft Graph API 获取 SharePoint Online 团队网站 [英] Using Microsoft Graph API to get SharePoint Online team sites
问题描述
我正在尝试访问组织的 SharePoint 团队网站.我正在使用 Microsoft Graph API,因为它是 Office 365 最完整的 API.我了解如何获取访问令牌以及如何使用它来发出请求.我知道它有效,因为我可以获得组列表,但是在获取所有团队网站时,我收到一条错误消息:
I'm trying to get access to SharePoint team sites of an organization. I'm using Microsoft Graph API as it's the most complete API for Office 365. I understand how to get an access token and how to use it to make requests. I know it works because I can get a list of groups but when it comes to get all team sites I get an error message:
Code : invalidRequest
Message : Cannot enumerate sites
这是我的代码.我正在控制台应用程序中对其进行测试.
Here is my code. I'm testing it out in a console application.
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;
}
}
我在谷歌上搜索了很多,但只找到了对我不起作用的解决方案.
I Googled a lot and only found solutions that didn't work for me.
推荐答案
该错误消息准确地说明了为什么这不起作用.
The error message is giving you an accurate description of why this isn't working.
对 graphClient.Sites.Request().GetAsync()
的调用被转换成 HTTP 调用 https://graph.microsoft.com/sites
'不是有效的 API 端点.
The call to graphClient.Sites.Request().GetAsync()
is translated into the HTTP call https://graph.microsoft.com/sites
which isn't a valid API endpoint.
您需要提供一些额外的上下文,例如您要查找的 site
.例如,要获取根站点,您可以调用:
You need to provide some additional context such as which site
you're looking for. For example, to get the Root Site you would call:
graphClient.Sites["root"].Request().GetAsync();
如果您正在寻找团队网站,您可以使用该网站的路径:
If you're looking for a Team Site you could use the site's path:
await graphClient.Sites["root"].SiteWithPath("/teams/myawesometeam").Request().GetAsync();
有关其他 SharePoint 端点,请参阅 图形 SharePoint 文档.
For additional SharePoint Endpoints, see the Graph SharePoint documentation.
这篇关于使用 Microsoft Graph API 获取 SharePoint Online 团队网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!