Since SharePoint 2013/Online supports a different client APIs there are several options to download a file from SharePoint depending on the API:
About SharePoint Online authentication
I guess your main concern is authentication in SPO. SharePoint Online (SPO) uses claims-based authentication mode. Microsoft released SharePoint Server 2013 Client Components SDK which contains SharePointOnlineCredentials class, below are provided some example that demonstrate how to utilize that class.
The following example demonstrates how to download a file from SPO using WebClient Class:
using (var client = new WebClient())
{
client.Credentials = GetSPOCredentials(webUri, userName, password);
client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
client.DownloadFile(sharePointFileUrl, localFileName);
}
where
public static SharePointOnlineCredentials GetSPOCredentials(Uri webUri, string userName, string password)
{
var securePassword = new SecureString();
foreach (var ch in password) securePassword.AppendChar(ch);
return new SharePointOnlineCredentials(userName, securePassword);
}
The following example demonstrates how to authenticate SharePoint Online Web Services:
using(var proxyLists = new Lists())
{
proxyLists.Url = webUri + "/_vti_bin/Lists.asmx";
proxyLists.CookieContainer = GetAuthCookies(webUri, userName, password);
//...
}
where
private static CookieContainer GetAuthCookies(Uri webUri, string userName, string password)
{
var securePassword = new SecureString();
foreach (var c in password) { securePassword.AppendChar(c); }
var credentials = new SharePointOnlineCredentials(userName, securePassword);
var authCookie = credentials.GetAuthenticationCookie(webUri);
var cookieContainer = new CookieContainer();
cookieContainer.SetCookies(webUri, authCookie);
return cookieContainer;
}
How to download a file using CSOM
SharePoint CSOM API contains File.OpenBinaryDirect method that is intended for downloading a file from SharePoint:
private static void DownloadFile(Web web, string fileUrl,string targetPath)
{
var ctx = (ClientContext)web.Context;
ctx.ExecuteQuery();
using(var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, fileUrl))
{
var fileName = Path.Combine(targetPath, System.IO.Path.GetFileName(fileUrl));
using (var fileStream = System.IO.File.Create(fileName))
{
fileInfo.Stream.CopyTo(fileStream);
}
}
}
Usage
using (var ctx = GetSPOContext(webUri,userName,password))
{
var web = ctx.Web;
DownloadFile(web, "/documents/SharePoint User Guide 2013.docx", @"c:\downloads");
}
where
public static ClientContext GetSPOContext(Uri webUri, string userName, string password)
{
var securePassword = new SecureString();
foreach (var ch in password) securePassword.AppendChar(ch);
return new ClientContext(webUri) { Credentials = new SharePointOnlineCredentials(userName, securePassword) };
}