user1711419
user1711419

Reputation:

Unable to download Office 365 SharePoint library item

I am trying to download item that exists in Office 365 SharePoint library using WebcClient.DownloadFile() but i am getting this exception:

Exception :

The remote server returned an error: (403) Forbidden.

Sample code :

NetworkCredential credential = new NetworkCredential("username", "password", "aaa.onmicrosoft.com");
WebClient webClient = new WebClient();
webClient.Credentials = credential;
webClient.DownloadFile(@"https://aaa.sharepoint.com/testDoc/test.pdf", @"c:/test.pdf");

Upvotes: 2

Views: 3475

Answers (3)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59318

Another option would be to utilize SharePointOnlineCredentials class from SharePoint Online Client Components SDK.

SharePointOnlineCredentials class represents an object that provides credentials to access SharePoint Online resources

Prerequisites

SharePoint Online Client Components SDK

How to download a file from SharePoint Online

 public static void DownloadFile(string userName, string password, string fileUrl, string filePath)
 {
        var securePassword = new SecureString();
        foreach (var c in password)
        {
            securePassword.AppendChar(c);
        }
        using (var client = new WebClient())
        {
            client.Credentials = new SharePointOnlineCredentials(userName, securePassword);
            client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
            client.DownloadFile(fileUrl, filePath);
        }
}

Upvotes: 6

andycwk
andycwk

Reputation: 846

With a bit of help from my friends, I've managed to crack this SharePoint on-line authentication stuff :)

I was kindly pointed in the direction of this blog post from Wictor Wilén.

And my WebClient call that uses Wictors claims library code...

var claimsHelper = new MsOnlineClaimsHelper(sharepointOnlineUrl, username, password);

var client = new WebClient();
client.Headers[ "Accept" ] = "/";
client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
client.Headers.Add(HttpRequestHeader.Cookie, claimsHelper.CookieContainer.GetCookieHeader(new Uri(sharepointOnlineUrl))  );

var document = client.DownloadString( documentUrl );

Upvotes: 2

Rob
Rob

Reputation: 3574

You need to add a little bit to get rid of your issue, called Headers and UserAgent.

 public static void method()
        {
         //   NetworkCredential myCredentials = new NetworkCredential("username", "password", "aaa.onmicrosoft.com");
            WebClient w = new WebClient();
            var ua = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
            w.Headers["Accept"] = "/";
            w.Headers.Add(HttpRequestHeader.UserAgent, ua);
            w.Credentials = myCredentials;
          w.DownloadFile(url, @"c:/name.doc"); 
        }

It downloads the file for me from the teamsite library in office 365. But it gives me a downloaded file. Only issue I have left : the file does not contain the real information you wish to download. I'm trying to solve this issue for a few days now - and this is the best result I've gotten up to now. Maybe you could help me on that with this new information. Let me know please :)

Upvotes: 2

Related Questions