简体   繁体   English

无法下载Office 365 SharePoint库项目

[英]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: 我试图使用WebcClient.DownloadFile()下载Office 365 SharePoint库中存在的项目,但我收到此异常:

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

Another option would be to utilize SharePointOnlineCredentials class from SharePoint Online Client Components SDK . 另一种选择是利用SharePoint Online Client Components SDK中的 SharePointOnlineCredentials类

SharePointOnlineCredentials class represents an object that provides credentials to access SharePoint Online resources SharePointOnlineCredentials类表示提供访问SharePoint Online资源的凭据的对象

Prerequisites 先决条件

SharePoint Online Client Components SDK SharePoint Online Client组件SDK

How to download a file from SharePoint Online 如何从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);
        }
}

You need to add a little bit to get rid of your issue, called Headers and UserAgent. 你需要添加一点来解决你的问题,称为Headers和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. 它从Office 365的teamsite库中为我下载文件。但是它给了我一个下载的文件。 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 :) 请告诉我:)

With a bit of help from my friends, I've managed to crack this SharePoint on-line authentication stuff :) 在朋友的帮助下,我成功破解了这个SharePoint在线身份验证的东西:)

I was kindly pointed in the direction of this blog post from Wictor Wilén. 我对WictorWilén撰写的这篇博客文章表示友好。

And my WebClient call that uses Wictors claims library code... 我的WebClient调用使用Wictors声明库代码...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM