c# - 无法下载 Office 365 SharePoint 库项目

标签 c# .net office365

我正在尝试使用 WebcClient.DownloadFile() 下载 Office 365 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");

最佳答案

另一种选择是利用 SharePointOnlineCredentials class来自 SharePoint Online Client Components SDK .

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

关于c# - 无法下载 Office 365 SharePoint 库项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12671194/

相关文章:

c# - 使用多个数据库与使用单个数据库的优缺点

c# - 使用 WinSCP .NET Assembly 防止通过 FTP 传输不完整的文件

.net - 使用对象作为字典键

sharepoint - 在 azure AD 中为 sharepoint online 创建一个应用程序

powerpoint - 获取 PowerPoint 内容插件的幻灯片索引或可见性状态 (Office.js)

c# - 在 C# 中使用 wcf 将 web url 作为参数传递到 web 服务中

c# - 如何获取报告服务实例上可用的报告列表

.net - Visual Studio 2010断点未加载任何符号

C# - 文件路径的正则表达式,例如C :\test\test. 可执行文件

c# - 适用于 SharePoint 的 Office 365 API