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

时间:2019-05-01 18:26:38

标签: c# microsoft-graph onedrive microsoft-graph-sdks

我正在尝试通过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” 。 p>

我不知道该如何进行,仍在研究中,但是文档目前让我一无所知。有人成功.Expand()或从云端硬盘获取了实际文件吗?

谢谢, 是

1 个答案:

答案 0 :(得分:2)

仅在获取单个Items时使用DriveItem

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

await graphClient
  .Drives[drive.Id]
  .Items[item.Id]
  .Request()
  .GetAsync();

要检索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();

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