我正在尝试使用WebcClient.DownloadFile()下载Office365 SharePoint库中存在的项目,但遇到此异常:
例外:
The remote server returned an error: (403) Forbidden.示例代码:
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");发布于 2012-10-31 20:52:35
在我朋友的帮助下,我成功地破解了这个SharePoint在线身份验证的东西:)
我被Wictor Wilén亲切地指着this blog post的方向。
我的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 );发布于 2014-08-28 19:17:12
另一种选择是利用SharePoint Online Client Components SDK的SharePointOnlineCredentials class。
SharePointOnlineCredentials class表示提供凭据以访问SharePoint Online资源的对象
先决条件
SharePoint Online Client Components SDK
如何从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);
}
}发布于 2012-10-03 15:43:33
您需要添加一点来解决您的问题,称为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");
}它会从office 365中的团队站点库中为我下载该文件。但它给了我一个下载的文件。我只剩下一个问题:该文件不包含您想要下载的真实信息。我试图解决这个问题已经有几天了--这是到目前为止我得到的最好的结果。也许你可以用这个新信息来帮助我。请让我知道:)
https://stackoverflow.com/questions/12671194
复制相似问题