Forum Discussion
JRDTechnologies
Sep 20, 2021Brass Contributor
How to download files from SharePoint online using the.net WebClient
I'm trying to download files from SharePoint online using the.net 4.8 WebClient and the Microsoft.SharePointOnline.CSOM package. Here is sample code, what am I doing wrong?
string uid = "myusername@domain.com"; string pwd = "mypassword"; string url = "https://tenant.sharepoint.com/:b:/r/filepath.pdf"; var sp = new SecureString(); foreach (var c in pwd.ToCharArray()) { sp.AppendChar(c); } var cred= new SharePointOnlineCredentials(uid, sp); using (var cl = new WebClient()) { cl.Credentials = credentials; cl.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f"); cl.DownloadFile(url, "C:\\temp\\Hello.docx); }
I'm getting a 401 Unauthorized error. To double-check that my credentials were right, I logged out and back into the SharePoint environment using the same credentials.
To test if it helps, I added a few of other user agents to the header.
cl.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); also tried... cl.Headers.Add("User-Agent: Other");
No RepliesBe the first to reply