microsoftgraph / microsoftgraph/msgraph-sdk-dotnet

File upload fails with ODataError: Invalid request

Open
#2,867 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Status: Needs investigation type:bug
Dominant language
C#
Stars
789
Forks
264
Avg merge
15h 17m
Merged PRs (30d)
3

Description

Describe the bug

I have a simple app to upload files form a specific folder to a specific OneDrive folder. This app has been working without issues for over 18 months. Since mid-February, it stopped working with this error:

Invalid Request.

Here my code snippet:

public static async Task SyncFileOld(string driveId, SyncItem syncItem)
     {
         try
         {
             // https://learn.microsoft.com/en-us/graph/sdks/large-file-upload?tabs=csharp
             var driveUpload = new DriveUpload.CreateUploadSessionPostRequestBody()
             {
                 Item = new DriveItemUploadableProperties
                 {
                     Name = Path.GetFileName(syncItem.Path),
                     AdditionalData = new Dictionary<string, object>
                     {
                         { "@microsoft.graph.conflictBehavior", "replace" },
                     },
                     FileSystemInfo = new Microsoft.Graph.Models.FileSystemInfo()
                     {
                         LastModifiedDateTime = File.GetLastWriteTimeUtc(syncItem.Path)
                     },
                     Description = "desc"
                 }
             };

             // note: this does not always return the correct info
             var response = await _graphServiceClient!
                 .Drives[driveId]
                 .Items[syncItem.FolderId]
                 .SearchWithQ(q: driveUpload.Item.Name)
                 .GetAsSearchWithQGetResponseAsync();

             if (response?.Value?.Count > 0)
             {
                 logger.Warn($"File {driveUpload.Item.Name} already exists. Skipping.");
                 return;
             }

             
// ==> this line throws the exception
var uploadSession = await _graphServiceClient!
                 .Drives[driveId]
                 .Items[syncItem.FolderId]
                 .ItemWithPath(driveUpload.Item.Name)
                 .CreateUploadSession
                 .PostAsync(driveUpload);


             logger.Trace($"Uploading {Path.GetFileName(syncItem.Path)} ...");

             using (var fileStream = File.OpenRead(syncItem.Path))
             {
                 int maxSliceSize = 320 * 1024 * 10;
                 var fileUploadTask = new LargeFileUploadTask<DriveItem>(
                     uploadSession, fileStream, maxSliceSize, _graphServiceClient.RequestAdapter);

                 var totalLength = fileStream.Length;
                 // Create a callback that is invoked after each slice is uploaded
                 IProgress<long> progress = new Progress<long>(prog =>
                 {
                     logger.Trace($"Uploaded {prog} bytes of {totalLength} bytes.");
                 });

                 var uploadResult = await fileUploadTask.UploadAsync(progress);

                 if (uploadResult.UploadSucceeded)
                 {
                     logger.Trace($"Uploaded {Path.GetFileName(syncItem.Path)} successfully.");
                 }
                 else
                 {
                     logger.Error($"Error in uploading {Path.GetFileName(syncItem.Path)}.");
                 }
             }

         }
         catch (Exception ex)
         {
             logger.Error(ex, $"Unable to upload file {syncItem.Path}.");
         }
     }

Variations: If I remove AdditionalData, I get a different error: 401 (access denied? not sure why). Nothing has changed in the app registration/permissions. I created a new app registration and still got the same error.

Expected behavior

The upload should work as expected.

How to reproduce
SDK Version

5.73.0

Latest version known to work for scenario above?

No response

Known Workarounds

None, to my knowledge.

Debug output
Click to expand log ```
</details>


### Configuration

- OS: Windows (11)
- .NET core 9.0

### Other information

_No response_

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the CreateUploadSession.PostAsync call in the supplied SyncFileOld method and compare it with the Microsoft Graph large-file-upload sample linked in the issue. Reproduce the upload using SDK version 5.73.0, then inspect the returned Invalid request and 401 responses. Done means identifying a reproducible cause and documenting or fixing the upload behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.