我使用sharpBox API连接到DropBox。它正确上传文件,但是当它尝试下载文件时会出现错误
远程服务器返回错误:(401)未经授权。
我的代码如下
using AppLimit.CloudComputing.SharpBox;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DropBox
{
public class Program
{
static void UploadDownloadProgress(Object sender, FileDataTransferEventArgs e)
{
// print a dot
Console.WriteLine("Downloading");
Console.WriteLine(".");
// it's ok to go forward
e.Cancel = false;
}
static void UploadProgress(Object sender, FileDataTransferEventArgs e)
{
// print a dot
Console.Write(".");
// it's ok to go forward
e.Cancel = false;
}
public static void DownloadFolder(CloudStorage dropBoxStorage, ICloudDirectoryEntry remoteDir, string targetDir, string sourceDir)
{
foreach (ICloudFileSystemEntry fsentry in remoteDir)
{
var filepath = Path.Combine(targetDir, fsentry.Name);
if (fsentry is ICloudDirectoryEntry)
{
Console.WriteLine("Created: {0}", filepath);
Directory.CreateDirectory(filepath);
DownloadFolder(dropBoxStorage, fsentry as ICloudDirectoryEntry, filepath, sourceDir);
}
else
{
dropBoxStorage.DownloadFile(remoteDir, fsentry.Name, targetDir, UploadDownloadProgress);
}
}
}
public static void UploadFile(CloudStorage dropBoxStorage, ICloudDirectoryEntry remoteDir, string targetDir)
{
dropBoxStorage.UploadFile(targetDir, remoteDir, UploadProgress);
}
static void Main(string[] args)
{
CloudStorage dropBoxStorage = new CloudStorage();
var dropBixConfig = CloudStorage.GetCloudConfigurationEasy(nSupportedCloudConfigurations.DropBox);
ICloudStorageAccessToken accessToken = null;
using (FileStream fs = File.Open(@"token.txt", FileMode.Open, FileAccess.Read, FileShare.None))
{
accessToken = dropBoxStorage.DeserializeSecurityToken(fs);
}
var storageToken = dropBoxStorage.Open(dropBixConfig, accessToken);
//do stuff
var root = dropBoxStorage.GetRoot();
var publicFolder = dropBoxStorage.GetFolder("/");
foreach (var folder in publicFolder)
{
Boolean bIsDirectory = folder is ICloudDirectoryEntry;
Console.WriteLine("{0}: {1}", bIsDirectory ? "DIR" : "FIL", folder.Name);
}
string remoteDirName = @"/Public";
string targetDir = "M://Drop";
var remoteDir = dropBoxStorage.GetFolder(remoteDirName);
DownloadFolder(dropBoxStorage, remoteDir, targetDir, remoteDirName);
dropBoxStorage.Close();
}
public delegate void FileOperationProgressChanged(object sender, FileDataTransferEventArgs e);
}
}