Dropbox API Unable to upload a file Issue while uploading

607 Views Asked by At

I use HigLabo.Net.Dropbox to upload a file to Dropbox. I created a App named synch and I am trying to upload a file. Below is my code

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

The code executes fine but the file is not getting upload in Dropbox.

Am I missing something? Is the path to upload correct? How do I view the file in Dropbox whether it is uploaded or not?

Is there a setting which I am missing while creating the app? I am just looking at the home page and I am expecting the file at the root folder. Am I correct?

Or do I need to look into some other location?

1

There are 1 best solutions below

0
On

Thanks @smarx and @Greg.

The below is the code to accomplish the task. Thanks again for your support, I hope this will be helpful for some one.

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