401 System.UnauthorizedAccessException 使用 SharpBox 访问 Dropbox 时 API

401 System.UnauthorizedAccessException when access Dropbox With SharpBox API

代码

config = CloudStorage.GetCloudConfigurationEasy(nSupportedCloudConfigurations.DropBox) 
    as DropBoxConfiguration;
//config.AuthorizationCallBack = new Uri("http://localhost:61926/DBoxDemo.aspx");

requestToken = DropBoxStorageProviderTools.GetDropBoxRequestToken(config, "KEY", "SECRET");
//Session["requestToken"] = requestToken;

string AuthoriationUrl = DropBoxStorageProviderTools.GetDropBoxAuthorizationUrl(
    config, requestToken);
Process.Start(AuthoriationUrl);
accessToken = DropBoxStorageProviderTools.ExchangeDropBoxRequestTokenIntoAccessToken(
    config, "xxxxxxxxxxxxx", "xxxxxxxxxxxxx", requestToken);

CloudStorage dropBoxStorage = new CloudStorage();

var storageToken = dropBoxStorage.Open(config, accessToken);
var publicFolder = dropBoxStorage.GetFolder("/");

// upload a testfile from temp directory into public folder of DropBox
String srcFile = Environment.ExpandEnvironmentVariables(@"C:\Test\MyTestFile.txt");
var rep = dropBoxStorage.UploadFile(srcFile, publicFolder);
MessageBox.Show("Uploaded Successfully..");

**dropBoxStorage.DownloadFile("/MyTestFile.txt",
Environment.ExpandEnvironmentVariables("D:\test"));**

MessageBox.Show("Downloaded Successfully..");
dropBoxStorage.Close();

这是 Visual Studio 中显示的错误。

SharpBox 有一个只出现在 .NET 4.5 中的错误,因为 class System.Uri 的行为已从 4.0 更改为 4.5。

DropBoxStorageProviderService.cs 中的方法 GetDownloadFileUrlInternal() 生成了不正确的 URL,因为它更改了 %2f 中的斜线。在 .NET 4.0 中,这个 URL 将通过 OAuthUrlGenerator.cs 中方法 GenerateSignedUrl() 中的 System.Uri 对象正确转换回来。 =17=]

我已经从这个改变了方法GetDownloadFileUrlInternal()...

public static String GetDownloadFileUrlInternal(IStorageProviderSession session, ICloudFileSystemEntry entry)
{
    // cast varibales
    DropBoxStorageProviderSession dropBoxSession = session as DropBoxStorageProviderSession;

    // gather information
    String rootToken = GetRootToken(dropBoxSession);
    String dropboxPath = GenericHelper.GetResourcePath(entry);

    // add all information to url;
    String url = GetUrlString(DropBoxUploadDownloadFile, session.ServiceConfiguration) + "/" + rootToken;

    if (dropboxPath.Length > 0 && dropboxPath[0] != '/')
        url += "/";

    url += HttpUtilityEx.UrlEncodeUTF8(dropboxPath);

    return url;
} 

...到此...

public static String GetDownloadFileUrlInternal(IStorageProviderSession session, ICloudFileSystemEntry entry)
{
    // cast varibales
    DropBoxStorageProviderSession dropBoxSession = session as DropBoxStorageProviderSession;

    // gather information
    String rootToken = GetRootToken(dropBoxSession);

    // add all information to url;
    String url = GetUrlString(DropBoxUploadDownloadFile, session.ServiceConfiguration) + "/" + rootToken;

    ICloudFileSystemEntry parent = entry.Parent;
    String dropboxPath = HttpUtilityEx.UrlEncodeUTF8(entry.Name);

    while(parent != null)
    {
        dropboxPath = HttpUtilityEx.UrlEncodeUTF8(parent.Name) + "/" + dropboxPath;
        parent = parent.Parent;
    }

    if (dropboxPath.Length > 0 && dropboxPath[0] != '/')
        url += "/";

    url += dropboxPath;

    return url;
}

目前它适用于 .NET 4.5。可能存在解决问题的更好方法,但目前未发现任何不当行为。