Yan*_*the 5 c# sharepoint-online microsoft-graph-api
我正在尝试访问组织的 SharePoint 团队网站。我正在使用 Microsoft Graph API,因为它是 Office 365 最完整的 API。我了解如何获取访问令牌以及如何使用它来发出请求。我知道它有效,因为我可以获得组列表,但是在获取所有团队网站时,我收到一条错误消息:
Code : invalidRequest
Message : Cannot enumerate sites
Run Code Online (Sandbox Code Playgroud)
这是我的代码。我正在控制台应用程序中对其进行测试。
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我在谷歌上搜索了很多,只找到了对我不起作用的解决方案。
错误消息为您提供了为什么这不起作用的准确描述。
调用graphClient.Sites.Request().GetAsync()被转换为 HTTP 调用https://graph.microsoft.com/sites,它不是有效的 API 端点。
您需要提供一些额外的上下文,例如site您正在寻找的上下文。例如,要获取根站点,您可以调用:
graphClient.Sites["root"].Request().GetAsync();
Run Code Online (Sandbox Code Playgroud)
如果您正在寻找团队网站,您可以使用该网站的路径:
await graphClient.Sites["root"].SiteWithPath("/teams/myawesometeam").Request().GetAsync();
Run Code Online (Sandbox Code Playgroud)
有关其他 SharePoint 终结点,请参阅图形 SharePoint 文档。
| 归档时间: |
|
| 查看次数: |
5208 次 |
| 最近记录: |