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

时间:2015-01-30 12:22:52

标签: asp.net dropbox sharpbox

代码

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中显示的错误。

Screenshot

1 个答案:

答案 0 :(得分:3)

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

DropBoxStorageProviderService.cs 中的方法GetDownloadFileUrlInternal()会生成错误的网址,因为它会更改%2f 中的斜杠。在.NET 4.0中,此URL将通过 OAuthUrlGenerator.cs 中方法System.Uri中的GenerateSignedUrl()对象正确转换回来。

我已经改变了方法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。它可能是解决问题的更好方法,但目前没有发现任何不当行为。