401 System.UnauthorizedAccessException when access Dropbox With SharpBox API

假如想象 提交于 2019-12-24 05:02:25

问题


The code

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();

This is the Error shown in Visual Studio.


回答1:


SharpBox has a bug that only occurs in .NET 4.5, because the behavior of the class System.Uri has changed from 4.0 to 4.5.

The method GetDownloadFileUrlInternal() in DropBoxStorageProviderService.cs generates an incorrect URL, because it changes a slash in %2f. In .NET 4.0, this URL will be converted correctly back through the System.Uri object in the method GenerateSignedUrl() in OAuthUrlGenerator.cs.

I have changed the method GetDownloadFileUrlInternal() from this...

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;
} 

...to this...

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;
}

and currently it works with .NET 4.5. It may exist a better way to fix the problem, but currently no misconduct noticed.



来源:https://stackoverflow.com/questions/28235866/401-system-unauthorizedaccessexception-when-access-dropbox-with-sharpbox-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!