简体   繁体   English

下载 SharePoint Office 365 版本文件

[英]Download version file of SharePoint office 365

I tried to download previous version files of SharePoint using c#.我尝试使用 c# 下载 SharePoint 的早期版本文件。 I have used This article as reference.我使用了这篇文章作为参考。 The link is working file with chrome.该链接是带有 chrome 的工作文件。 Now when I tried the URL on c# to download the file part by part, it is giving me The remote server returned an error: (401) Unauthorized.现在,当我尝试使用 c# 上的 URL 部分下载文件时,它给我远程服务器返回错误:(401) 未经授权。 error.错误。

I have even provided access token using to header of the function.我什至提供了使用 to 函数标头的访问令牌。

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            WebHeaderCollection header = new WebHeaderCollection();
            request.Headers.Add(System.Net.HttpRequestHeader.Authorization, $"Bearer {token}");
            request.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");

Here, uri is similar to the address: http://yoursite/yoursubsite/_vti_history/512/Documents/Book1.xlsx这里,uri类似于地址: http://yoursite/yoursubsite/_vti_history/512/Documents/Book1.xlsx

How can I download the previous version file using c#?如何使用 c# 下载以前的版本文件?

Here is my test code for your reference.这是我的测试代码供您参考。

var login = "user@xxx.onmicrosoft.com";
            var password = "Password";

            var securePassword = new SecureString();

            foreach (char c in password)
            {
                securePassword.AppendChar(c);
            }
            SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword);

            string webUrl = "https://xxx.sharepoint.com/sites/lee";
            string requestUrl = "https://xxx.sharepoint.com/sites/lee/_vti_history/512/MyDoc2/testdata.xlsx";
            Uri uri = new Uri(requestUrl);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "GET";
            request.Credentials = onlineCredentials;
            request.Headers[HttpRequestHeader.Cookie] = onlineCredentials.GetAuthenticationCookie(new Uri(webUrl), true);  // SPO requires cookie authentication
            request.Headers["X-FORMS_BASED_AUTH_ACCEPTED"] = "f";  // disable interactive forms-based auth            
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream stream = response.GetResponseStream();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM