using System; using System.Net; using System.Net.Security; using System.Security; using System.Data; using System.Collections.Generic; using System.Linq; using System.Text; //using System.Windows.Forms; using Microsoft.SharePoint.Client; namespace OnlineDownLoadConsoleApp { class Program { static void Main(string[] args) { //System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12; ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072; DownloadOnlineFile(); } public static void DownloadOnlineFile() { byte[] emptyBytes = new byte[0]; string username = "account@domain.xyz"; string password = "PASSWORD"; string url = "https://xyz.sharepoint.com/sites/Sitename/Shared%20Documents/File.xlsx"; string FilePath = "c:\\temp\\File.xlsx"; var securedPassword = new SecureString(); foreach (var c in password.ToCharArray()) securedPassword.AppendChar(c); var credentials = new SharePointOnlineCredentials(username, securedPassword); try { DownloadFile(url, credentials, FilePath); } catch (Exception ex) { Console.WriteLine("ex: " + ex.Message + " Innerex:" + ex.InnerException.Message); } } private static void DownloadFile(string webUrl, ICredentials credentials, string fileRelativeUrl) { byte[] emptyBytes = new byte[0]; try { using (var client = new WebClient()) { client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f"); client.Headers.Add("User-Agent: Other"); client.Credentials = credentials; client.DownloadFile(webUrl, fileRelativeUrl); } } catch (Exception ex) { Console.WriteLine("ex: " + ex.Message + " Innerex:" + ex.InnerException.Message); } } } }