Sep 20 2021
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 = "email@example.com";
string pwd = "mypassword";
string url = "https://tenant.sharepoint.com/:b:/r/filepath.pdf";
var sp = new SecureString();
foreach (var c in pwd.ToCharArray())
var cred= new SharePointOnlineCredentials(uid, sp);
using (var cl = new WebClient())
cl.Credentials = credentials;
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;)");