从 SharePoint 365 下载文件 [英] Download File From SharePoint 365

查看:31
本文介绍了从 SharePoint 365 下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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}" .......

", 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("
Downloaded file saved in the following file system folder:
	" + Application.StartupPath);

我正在使用 MSDN 中的代码网站

但是我遇到了错误:403 forbidden

But I have coming across the error: 403 forbidden

有人可以帮我解决这个问题吗?

Can someone Help me Put this working ?

推荐答案

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

I was facing the same issue and tried the answer suggested by Vadim Gremyachev. However, it still kept giving 403 error. I added two extra headers to force form based authentication like below:

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

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

After this it started working. So the full code goes like below:

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

这篇关于从 SharePoint 365 下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆