我正在尝试下载Office 365 SharePoint库中存在的项目,WebcClient.DownloadFile()但我收到此异常:
例外:
The remote server returned an error: (403) Forbidden.
Run Code Online (Sandbox Code Playgroud)
示例代码:
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");
Run Code Online (Sandbox Code Playgroud)
另一种选择是利用SharePointOnlineCredentials类从 SharePoint Online的客户端组件SDK.
SharePointOnlineCredentials类表示提供访问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);
}
}
Run Code Online (Sandbox Code Playgroud)