c# - Microsoft Graph API - 发送读取文件的请求时出错
问题描述
我正在尝试使用官方 SDK 和OneDrive API 浏览器示例中的代码从 OneDrive 读取文件,如下所示。
var stream = await graphClient.Drives[drive.Id].Items[item.Id].Content.Request().GetAsync();
其中drive.Id
和item.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()
我该如何解决这个问题?
解决方案
尝试这个 -
// 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);
推荐阅读
- typescript - 对象破坏导致 TypeScript 中的“从不”类型
- dart - 错误:实例成员 ... 无法在初始化程序中访问
- r - 多列的错误 Shapiro-Wilk 测试
- python - 当我使用带有条件的 Pandas DataFrame DROP 方法时,如何摆脱烦人的警告?
- java - 我可以在 Java 高级别的客户端 elasticsearch 中使用 cookie 身份验证吗?
- postgresql - 仅查询索引列时,索引扫描将进入堆以获取结果
- javascript - 如何使我的移动菜单栏“显示:无”“显示:阻止”工作
- sql - 根据列中的日期填充代理 Datekey
- python - 使用来自 Statsmodel 实例的 preds 从 sklearn 获取 AUC
- rust - 从两个绝对路径中找到相对路径