Dropbox API上传

时间:2015-09-15 08:02:31

标签: c# dropbox dropbox-api higlabo

我使用HigLabo.Net.Dropbox将文件上传到Dropbox。我创建了一个名为synch的应用程序,我正在尝试上传文件。以下是我的代码

 byte[] bytes = System.IO.File.ReadAllBytes(args[1]);   
 UploadFile(bytes,"sundas.jpg","/Apps/synch/");   


public static void UploadFile(byte[] content, string filename, string target)
    {
        string App_key = "xxxxxxxxxxxxxxx";
        string App_secret = "yyyyyyyyyyyyyy";
        HigLabo.Net.OAuthClient ocl = null;
        HigLabo.Net.AuthorizeInfo ai = null;                    
        ocl = HigLabo.Net.Dropbox.DropboxClient.CreateOAuthClient(App_key, App_secret);                        
        ai = ocl.GetAuthorizeInfo();             
        string RequestToken= ai.RequestToken;
        string RequestTokenSecret= ai.RequestTokenSecret;
        string redirect_url = ai.AuthorizeUrl;
        AccessTokenInfo t = ocl.GetAccessToken(RequestToken, RequestTokenSecret); 
        string Token= t.Token;
        string TokenSecret= t.TokenSecret;
        DropboxClient cl = new DropboxClient(App_key, App_secret, Token, TokenSecret); 
        HigLabo.Net.Dropbox.UploadFileCommand ul = new HigLabo.Net.Dropbox.UploadFileCommand();
        ul.Root = RootFolder.Sandbox;

        Console.WriteLine(ul.Root);
        ul.FolderPath = target;
        ul.FileName = filename;
        ul.LoadFileData(content); 
        Metadata md = cl.UploadFile(ul);
        Console.WriteLine("END");
    }

代码执行正常,但文件没有在Dropbox中上传。

我错过了什么吗?上传的路径是否正确?如何在Dropbox中查看文件是否已上传?

创建应用时是否存在我缺少的设置?我只是看主页,我期待根文件夹中的文件。我是对的吗?

或者我是否需要查看其他位置?

1 个答案:

答案 0 :(得分:1)

谢谢@smarx和 @格雷格。

以下是完成任务的代码。再次感谢您的支持,我希望这对某些人有所帮助。

string filePath="C:\\Tim\\sundar.jpg";
RestClient client = new RestClient("https://api-content.dropbox.com/1/");
IRestRequest request = new RestRequest("files_put/auto/{path}", Method.PUT);
FileInfo fileInfo = new FileInfo(filePath);
long fileLength = fileInfo.Length;
request.AddHeader("Authorization", "Bearer FTXXXXXXXXXXXXXXXXXXXisqFXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
request.AddHeader("Content-Length", fileLength.ToString());
request.AddUrlSegment("path", string.Format("Public/{0}", fileInfo.Name));
byte[] data = File.ReadAllBytes(filePath);
var body = new Parameter
{
    Name = "file",
    Value = data,
    Type = ParameterType.RequestBody,
};
request.Parameters.Add(body);
IRestResponse response = client.Execute(request);