Upload a file to SharePoint Online using Graph access denied issue

iz111 86 Reputation points
2023-03-03T10:27:30.94+00:00

I have client application in C# to connect to SharePoint Online using Microsoft Graph.
This is code to upload a file to SharePoint Documents folder:

             var fileInfo = new FileCreationInformation()
                {
                    Content = fileContent,
                    Url = fileUrl,
                    Overwrite = true
                };
                var uploadFile = list.RootFolder.Files.Add(fileInfo);
                ctx.Load(uploadFile);
                ctx.ExecuteQuery();

however, I always get access denied error.
Note that i got access token and I can list files in Documents:

                var list = ctx.Web.Lists.GetByTitle("Documents");
                ctx.Load(list);
                ctx.Load(list.RootFolder);
                ctx.ExecuteQuery();

                // Get the collection of files in the library
                var files = list.RootFolder.Files;
                ctx.Load(files);
                ctx.ExecuteQuery();

                // Print the names of the files
                foreach (var file in files)
                {
                    Console.WriteLine(file.Name);
                }

Also, I've added following permissions in my Azure app:
Files.ReadWrite.All,
Sites.ReadWrite.All

For the scope i use something like this:
"https://<tenant domain>/.default"

What am I missing here? How to solve access denied erro on upload?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,447 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,300 questions
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 33,641 Reputation points Microsoft Vendor
    2023-03-07T07:36:44.9133333+00:00

    HI @iz111,

    I'm glad to hear you solve the problem ,if you have any issue about SharePoint, you are welcome to raise a ticket in this forum.

    By the way, since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others." and according to the scenario introduced here: Answering your own questions on Microsoft Q&A, I would make a brief summary of this thread:

    [Upload a file to SharePoint Online using Graph access denied issue]

    Issue Symptom:

    Upload a file to SharePoint Online using Graph by bearer token. Returned access denied error.

    Solution:

    The permission granted in Azure app delaied. The code works properly after the permission applied. Per my research, the permission will takes minutes to hours to active. It depends on the size of your tenant, the big tenant takes much longer than the time of the small tenant.


    You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community member's to see the useful information when reading this thread. Thanks for your understanding!

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. iz111 86 Reputation points
    2023-03-07T07:21:48.1866667+00:00

    It seems that above code works properly.
    Seems that permissions in my Azure app were applied with some delay.

    0 comments No comments