Microsoft Graph 错误 - 将文件上传到 OneDrive 时找不到段 'root:' 的资源

Microsoft Graph Error - Resource not found for the segment 'root:' when uploading file to OneDrive

在 Microsoft Graph OneDrive 团队的 Uploading File to OneDrive 上处理 this tutorial 时,我在下面显示的代码的最后一行收到以下错误:

备注:网上有一些帖子有相关问题,(如:, or , or this or this or )。但是他们似乎都有不同的背景或没有回应。

问题:可能是什么问题,我们该如何解决

Resource not found for the segment 'root:'

相关代码:

GraphServiceClient graphClient = ProviderManager.Instance.GlobalProvider.Graph;

var picker = new Windows.Storage.Pickers.FileOpenPicker();
....
picker.FileTypeFilter.Add("*");

Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
    // Application now has read/write access to the picked file
    // request 1 - upload small file to user's onedrive

  Stream fileStream = await file.OpenStreamForReadAsync();
  DriveItem uploadedFile = await graphClient.Me.Drive.Root
                                .ItemWithPath(file.Path)
                                .Content
                                .Request()
                                .PutAsync<DriveItem>(fileStream);
}

.ItemWithPath(file.Path) 不是您要上传的文件的路径,而是目标路径。

例如,如果您想将“SomeFile.txt”上传到 OneDrive 的根目录,您可以使用:

graphClient.Me.Drive // The drive
  .Root // The drive's root folder
  .ItemWithPath("SomeFile.txt") // The destination to write the upload to

当前失败的原因是 OneDrive 不知道如何处理 Windows 驱动器路径(即 C:\Files\Documents\SomeFile.txt)。它需要 URL 安全驱动器路径(即 /Documents/SomeFile.txt)。