I am trying to upload a document to the document library using App Based Token authentication in SharePoint Online.

I have referred following link to set up the App Based token and the token successfull worked when I try to read any thing from the site.

But when I try to upload a file using following code,

string file = @"Path Of the file";
System.IO.FileStream fs = new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read);
using (var ctx = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, "CLIENTID","CLIENTSECRET"))
        ctx.Load(ctx.Web, p => p.Title);
                     Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, "RelativePathToDocumentLibrary" + "/test.docx", fs, false);
                catch (Exception ex)

It gives me error of

{"The remote server returned an error: (401) Unauthorized."}

Any suggestion or reference link is most welcome.


The issue is that the SaveBinaryDirect method does not support app-based authentication. You should use either SharePointOnlineCredentials for your ClientContext or upload files with FileCreationInformation:

FileCreationInformation fci = new FileCreationInformation();
fci.Content = data;
fci.Url = url;

Microsoft.SharePoint.Client.File file = list.RootFolder.Files.Add(fci);
| improve this answer | |
  • Thanks for the quick help. It worked. Do we have any reference document link from where I can know what operations I cannot do using app-based token? – Aakash Maurya Aug 22 '19 at 6:52
  • Not so many, I do know about two of such methods: File.SaveBinaryDirect and File.OpenBinaryDirect. But have never seen any official documents pointing to those limitations. – Dmitry Kozlov Aug 22 '19 at 6:58
  • 1
    Here is the reference that I was looking for docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/… – Aakash Maurya Aug 22 '19 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.