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"))
{
try
{
ctx.Load(ctx.Web, p => p.Title);
ctx.ExecuteQuery();
Console.WriteLine(ctx.Web.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.