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.

1 Answer 1


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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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