How to download files from SharePoint online using the.net WebClient

%3CLINGO-SUB%20id%3D%22lingo-sub-2766300%22%20slang%3D%22en-US%22%3EHow%20to%20download%20files%20from%20SharePoint%20online%20using%20the.net%20WebClient%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2766300%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20trying%20to%20download%20files%20from%20SharePoint%20online%20using%20the.net%204.8%20WebClient%20and%20the%20Microsoft.SharePointOnline.CSOM%20package.%20Here%20is%20sample%20code%2C%20what%20am%20I%20doing%20wrong%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CSPAN%20class%3D%22%22%3Estring%3C%2FSPAN%3E%20uid%20%3D%20%3CSPAN%20class%3D%22%22%3E%22myusername%40domain.com%22%3C%2FSPAN%3E%3B%0A%3CSPAN%20class%3D%22%22%3Estring%3C%2FSPAN%3E%20pwd%20%3D%20%3CSPAN%20class%3D%22%22%3E%22mypassword%22%3C%2FSPAN%3E%3B%0A%3CSPAN%20class%3D%22%22%3Estring%3C%2FSPAN%3E%20url%20%3D%20%3CSPAN%20class%3D%22%22%3E%22https%3A%2F%2Ftenant.sharepoint.com%2F%3Ab%3A%2Fr%2Ffilepath.pdf%22%3C%2FSPAN%3E%3B%0A%3CSPAN%20class%3D%22%22%3Evar%3C%2FSPAN%3E%20sp%20%3D%20%3CSPAN%20class%3D%22%22%3Enew%3C%2FSPAN%3E%20SecureString()%3B%0A%3CSPAN%20class%3D%22%22%3Eforeach%3C%2FSPAN%3E%20(%3CSPAN%20class%3D%22%22%3Evar%3C%2FSPAN%3E%20c%20%3CSPAN%20class%3D%22%22%3Ein%3C%2FSPAN%3E%20pwd.ToCharArray())%0A%7B%0A%20%20%20%20sp.AppendChar(c)%3B%0A%7D%0A%3CSPAN%20class%3D%22%22%3Evar%3C%2FSPAN%3E%20cred%3D%20%3CSPAN%20class%3D%22%22%3Enew%3C%2FSPAN%3E%20SharePointOnlineCredentials(uid%2C%20sp)%3B%0A%3CSPAN%20class%3D%22%22%3Eusing%3C%2FSPAN%3E%20(%3CSPAN%20class%3D%22%22%3Evar%3C%2FSPAN%3E%20cl%20%3D%20%3CSPAN%20class%3D%22%22%3Enew%3C%2FSPAN%3E%20WebClient())%0A%7B%0A%20%20%20%20cl.Credentials%20%3D%20credentials%3B%0A%20%20%20%20cl.Headers.Add(%3CSPAN%20class%3D%22%22%3E%22X-FORMS_BASED_AUTH_ACCEPTED%22%3C%2FSPAN%3E%2C%20%3CSPAN%20class%3D%22%22%3E%22f%22%3C%2FSPAN%3E)%3B%0A%20%20%20%20cl.DownloadFile(url%2C%20%3CSPAN%20class%3D%22%22%3E%22C%3A%5C%5Ctemp%5C%5CHello.docx%3C%2FSPAN%3E)%3B%20%0A%7D%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20getting%20a%20401%20Unauthorized%20error.%20To%20double-check%20that%20my%20credentials%20were%20right%2C%20I%20logged%20out%20and%20back%20into%20the%20SharePoint%20environment%20using%20the%20same%20credentials.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20test%20if%20it%20helps%2C%20I%20added%20a%20few%20of%20other%20user%20agents%20to%20the%20header.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Ecl.Headers.Add(%3CSPAN%20class%3D%22%22%3E%22user-agent%22%3C%2FSPAN%3E%2C%20%3CSPAN%20class%3D%22%22%3E%22Mozilla%2F4.0%20(compatible%3B%20MSIE%206.0%3B%20Windows%20NT%205.2%3B%20.NET%20CLR%201.0.3705%3B)%22%3C%2FSPAN%3E)%3B%0A%3CSPAN%20class%3D%22%22%3Ealso%20tried...%3C%2FSPAN%3E%0Acl.Headers.Add(%3CSPAN%20class%3D%22%22%3E%22User-Agent%3A%20Other%22%3C%2FSPAN%3E)%3B%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

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

 

0 Replies