用c#在windows中连接到oneDrive
本文关键字:连接 oneDrive windows | 更新日期: 2023-09-27 18:13:52
我有一个c# Windows Forms应用程序,我想上传一个XML文件到OneDrive并下载到我的应用程序。
我读了MSDN关于Microsoft.Live
库,但我找不到任何东西,或者至少我不明白我应该做什么。有人能给我指个正确的方向吗?
请参考下面的示例代码,它可以帮助您连接一个驱动器并访问其文件和文件夹。
var optConfig = new Options
{
ClientId = "0000000000000000",
ClientSecret = "XXXXt02UYkQm6Kb48fKtD7",
CallbackUrl = "https://login.live.com/oauth20_desktop.srf",
AutoRefreshTokens = true,
PrettyJson = false,
AccessToken = "AccessTokenXXXXX",
RefreshToken = "RefreshTokenXXXX",
ReadRequestsPerSecond = 2,
WriteRequestsPerSecond = 2
};
var client = new Client(optConfig);
var rootFolder = await client.GetFolderAsync();
// rootFolder.Name - folder name
//rootFolder.Id - folder id
var folderContent = await client.GetContentsAsync(rootFolder.Id);
foreach (var item in folderContent)
{
Console.WriteLine("'tItem ({0}: {1} (Id: {2})", item.Type, item.Name, item.Id);
var folderContent = await client.GetContentsAsync(item.Id);
var filesList = folderContent.Where(e => e.Type != "folder");
foreach (var fileItem in filesList)
{
//Download files here
var uri = fileItem.Source;
var request = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
if ((response.StatusCode == HttpStatusCode.OK ||
response.StatusCode == HttpStatusCode.Moved ||
response.StatusCode == HttpStatusCode.Redirect))
{
var tempFileName = response.ResponseUri.Segments[(response.ResponseUri.Segments.Count() - 1)];
using (var fileStream = response.GetResponseStream())
{
//Save Strame
}
}
}
}