'Microsoft Graph API - An error occurred sending the request on reading file
I m trying to read a file from OneDrive using official SDK and a code from OneDrive API Browser Sample as follows.
var stream = await graphClient.Drives[drive.Id].Items[item.Id].Content.Request().GetAsync();
Where drive.Id and item.Id are valid IDs retrieved by previous successful calls to Graph.
I receive the following error.
An error occurred sending the request. Microsoft.Graph.ServiceException at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
Another time I got more detailed exception.
at Microsoft.Graph.HttpProvider.d__21.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.HttpProvider.d__20.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.HttpProvider.d__19.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.BaseRequest.d__36.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Graph.BaseRequest.d__34.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
How can I resolve this issue?
Solution 1:[1]
Try this -
// 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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shweta |
