从 Microsoft Graph API 获取驱动器的项目:请求格式错误或不正确

Yve*_*lpe 2 c# onedrive microsoft-graph-sdks microsoft-graph-api

我正在尝试通过 Microsoft Graph Api (SDK) 获取驱动器的项目并尝试了以下选项:

  1. _graphServiceClient.Drives[driveInfo.Id].Items.Request().GetAsync():,不幸的是,这会导致错误消息错误消息“请求格式错误或不正确”和代码“invalidRequest”_graphServiceClient.Drives[driveInfo.Id].Request().GetAsync()但是,如果我执行,我会取回所有驱动器,但Items属性为null.

  2. _graphServiceClient.Drives[driveInfo.Id].Request().Expand(d => d.Items).GetAsync(),这也会导致错误消息“请求格式错误或不正确”和代码“invalidRequest”

我不知道如何从这里继续,仍在研究中,但目前文档让我一无所知。任何人都成功.Expand()或从云端硬盘获取实际文件?

谢谢,是

Mar*_*eur 7

您仅Items在获取单个时使用DriveItem

await graphClient
  .Me
  .Drive
  .Items[item.Id] 
  .Request()
  .GetAsync();

await graphClient
  .Drives[drive.Id]
  .Items[item.Id]
  .Request()
  .GetAsync();
Run Code Online (Sandbox Code Playgroud)

当您要检索一个DriveItem集合时,您需要指定根文件夹:

await graphClient
  .Me
  .Drive
  .Root // <-- this is the root of the drive itself
  .Children // <-- this is the DriveItem collection
  .Request()
  .GetAsync();

await graphClient
  .Drives[drive.Id]
  .Root 
  .Children
  .Request()
  .GetAsync();
Run Code Online (Sandbox Code Playgroud)

SDK 单元测试是快速示例的良好来源。例如,OneDriveTests.cs包含几个用于寻址 Drives 和 DriveItems 的示例。

  • OneDriveTests.cs 文件现在似乎已移至此处:https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/tests/Microsoft.Graph.DotnetCore.Test/Requests/Functional/OneDriveTests.cs (答案404底部的链接)。然而,您的回答使我最终能够检索到驱动器项目 - 非常感谢您! (2认同)