【问题标题】:Download File From SharePoint 365从 SharePoint 365 下载文件
【发布时间】:2014-05-15 16:39:22
【问题描述】:
string remoteUri = "http://www.contoso.com/library/homepage/images/";
            string fileName = "ms-banner.gif", myStringWebResource = null;
            // Create a new WebClient instance.
            WebClient myWebClient = new WebClient();
            // Concatenate the domain with the Web resource filename.
            myStringWebResource = remoteUri + fileName;
            Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
            // Download the Web resource and save it into the current filesystem folder.
            myWebClient.DownloadFile(myStringWebResource,fileName);     
            Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
            Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);

我正在使用来自MSDN Web Site的代码

但我遇到了错误:403 禁止

有人可以帮我把这个工作吗?

【问题讨论】:

  • 为什么你下载的文件是认证类型?您是否尝试将myWebClient.Credentials = new NetworkCredential("User","Pass"); 设置为基本身份验证?
  • 尝试凭据后出现同样的错误!

标签: c# sharepoint office365


【解决方案1】:

我遇到了同样的问题,并尝试了 Vadim Gremyachev 建议的答案。但是,它仍然不断给出 403 错误。我添加了两个额外的标题来强制基于表单的身份验证,如下所示:

client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
client.Headers.Add("User-Agent: Other");

在此之后它开始工作。所以完整的代码如下:

const string username = "username@tenant.onmicrosoft.com";
const string password = "password";
const string url = "https://tenant.sharepoint.com/";
var securedPassword = new SecureString();
foreach (var c in password.ToCharArray()) securedPassword.AppendChar(c);
var credentials = new SharePointOnlineCredentials(username, securedPassword);

DownloadFile(url,credentials,"/Shared Documents/Report.xslx");


private static void DownloadFile(string webUrl, ICredentials credentials, string fileRelativeUrl)
{
     using(var client = new WebClient())
     {
        client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
        client.Headers.Add("User-Agent: Other");
        client.Credentials = credentials;
        client.DownloadFile(webUrl, fileRelativeUrl);
     }  
}

【讨论】:

    【解决方案2】:

    由于请求未经过身份验证而发生此错误。为了访问 Office/SharePoint Online 中的资源,您可以使用来自 SharePoint Server 2013 Client Components SDKSharePointOnlineCredentials class用户凭据流程)。

    以下示例演示如何从 SPO 下载文件:

    const string username = "username@tenant.onmicrosoft.com";
    const string password = "password";
    const string url = "https://tenant.sharepoint.com/";
    var securedPassword = new SecureString();
    foreach (var c in password.ToCharArray()) securedPassword.AppendChar(c);
    var credentials = new SharePointOnlineCredentials(username, securedPassword);
    
    DownloadFile(url,credentials,"/Shared Documents/Report.xslx");
    
    
    private static void DownloadFile(string webUrl, ICredentials credentials, string fileRelativeUrl)
    {
         using(var client = new WebClient())
         {
            client.Credentials = credentials;
            client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
            client.DownloadFile(webUrl, fileRelativeUrl);
         }  
    }
    

    【讨论】:

    • 我在上面的代码中得到 403 异常。还要注意,fileRelativeUrl 应该是保存文件的本地系统路径。
    • 调用client.DownloadFile时使用fileRelativeURL的代码不正确。运行此代码会导致与 OP 报告类似的 403 错误。从这个意义上说,这不是最好的答案。
    • @Kris,感谢您的建设性评论。该错误确实可能发生,因为 SharePoint 还需要在请求中提供额外的标头:X-FORMS_BASED_AUTH_ACCEPTED:f
    猜你喜欢
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 2019-05-09
    相关资源
    最近更新 更多