1

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?

8
  • Do you get any errors, returned data from API requests? Commented Sep 15, 2015 at 8:07
  • Thanks for the message. I got {"error": "Authentication failed"} when printed it....Not sure which authentication failed. I am using the secret key from the drop box browser. Is there some more authentication I need to give? Commented Sep 15, 2015 at 10:32
  • 1
    I am following this link codingstill.com/2013/11/…. I am not sure where I need/how I need to authorize. Any inputs please Commented Sep 15, 2015 at 10:44
  • Got to know that I need to manually authorize by supplying my dropbox credentials. I need this to be automated. I knew the access code. Is there a way to authorize it through the code and avoid the manual intervention through the browser? Commented Sep 15, 2015 at 11:17
  • You only to need to auth once. From then on, you can just store and reuse the same access token over and over. If this is just for you to use, you can generate an OAuth 2 access token from the App Console, but it looks like the library you're using may still use OAuth 1 instead of OAuth 2. Commented Sep 15, 2015 at 17:16

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.