首页 > 解决方案 > Microsoft Graph API - 发送读取文件的请求时出错

问题描述

我正在尝试使用官方 SDK 和OneDrive API 浏览器示例中的代码从 OneDrive 读取文件,如下所示。

var stream = await graphClient.Drives[drive.Id].Items[item.Id].Content.Request().GetAsync();

其中drive.Iditem.Id是先前成功调用 Graph 检索到的有效 ID。

我收到以下错误。

发送请求时出错。System.Threading.Tasks.Task`1.GetResultCore 处的 Microsoft.Graph.ServiceException(布尔值 waitCompletionNotification)

还有一次我得到了更详细的例外。

在 Microsoft.Graph.HttpProvider.d__21.MoveNext() 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 Microsoft.Graph.HttpProvider.d__20.MoveNext()在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 Microsoft.Graph.HttpProvider.d__19.MoveNext()
在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 Microsoft.Graph.BaseRequest.d__36.MoveNext() 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task任务)在 Microsoft.Graph.BaseRequest.d__34.MoveNext()
在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在 System.Runtime.CompilerServices.TaskAwaiter`1 .GetResult()

我该如何解决这个问题?

标签: c#microsoft-graph-api

解决方案


尝试这个 -

// get reference to stream of file in OneDrive
GraphServiceClient graphClient = GetAuthenticatedGraphClient(...);
var fileStream = graphClient.Drives[drive.Id].Items[fileId]
                                     .Content
                                     .Request()
                                     .GetAsync()
                                     .Result;

var currentFolder = System.IO.Directory.GetCurrentDirectory();
var driveItemPath = Path.Combine(currentFolder, "proposal.docx");

// save stream to the local file
var driveItemFile = System.IO.File.Create(driveItemPath);
fileStream.Seek(0, SeekOrigin.Begin);
fileStream.CopyTo(driveItemFile);

推荐阅读