首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法下载Office 365 SharePoint库项目

无法下载Office 365 SharePoint库项目
EN

Stack Overflow用户
提问于 2012-10-01 18:03:41
回答 3查看 3.4K关注 0票数 2

我正在尝试使用WebcClient.DownloadFile()下载Office365 SharePoint库中存在的项目,但遇到此异常:

例外:

代码语言:javascript
复制
The remote server returned an error: (403) Forbidden.

示例代码:

代码语言:javascript
复制
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");
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-10-31 20:52:35

在我朋友的帮助下,我成功地破解了这个SharePoint在线身份验证的东西:)

我被Wictor Wilén亲切地指着this blog post的方向。

我的WebClient调用使用了Wictors声明库代码...

代码语言:javascript
复制
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 );
票数 2
EN

Stack Overflow用户

发布于 2014-08-28 19:17:12

另一种选择是利用SharePoint Online Client Components SDKSharePointOnlineCredentials class

SharePointOnlineCredentials class表示提供凭据以访问SharePoint Online资源的对象

先决条件

SharePoint Online Client Components SDK

如何从SharePoint Online下载文件

代码语言:javascript
复制
 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);
        }
}
票数 6
EN

Stack Overflow用户

发布于 2012-10-03 15:43:33

您需要添加一点来解决您的问题,称为Headers和UserAgent。

代码语言:javascript
复制
 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"); 
        }

它会从office 365中的团队站点库中为我下载该文件。但它给了我一个下载的文件。我只剩下一个问题:该文件不包含您想要下载的真实信息。我试图解决这个问题已经有几天了--这是到目前为止我得到的最好的结果。也许你可以用这个新信息来帮助我。请让我知道:)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12671194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档