从SharePoint 365下载文件

Nev*_*ito 5 c# sharepoint office365

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);
Run Code Online (Sandbox Code Playgroud)

我在MSDN网站上使用此代码

但我遇到了错误:403被禁止

有人可以帮助我这个有用吗?

bla*_*cer 9

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

client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
client.Headers.Add("User-Agent: Other");
Run Code Online (Sandbox Code Playgroud)

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

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);
     }  
}
Run Code Online (Sandbox Code Playgroud)


Vad*_*hev 8

由于请求未经过身份验证,因此会发生此错误.为了访问在办公室/资源的SharePoint Online,您可以利用SharePointOnlineCredentials类SharePoint服务器2013客户端组件SDK(用户凭证流).

以下示例演示了如何从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);
     }  
}
Run Code Online (Sandbox Code Playgroud)