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?

Greg
  • 16,359
  • 2
  • 34
  • 44
Timothy Rajan
  • 1,947
  • 8
  • 38
  • 62
  • Do you get any errors, returned data from API requests? – Callum Linington Sep 15 '15 at 08: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? – Timothy Rajan Sep 15 '15 at 10:32
  • 1
    I am following this link http://codingstill.com/2013/11/use-dropbox-with-your-asp-net-application/. I am not sure where I need/how I need to authorize. Any inputs please – Timothy Rajan Sep 15 '15 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? – Timothy Rajan Sep 15 '15 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](https://blogs.dropbox.com/developers/2014/05/generate-an-access-token-for-your-own-account/), but it looks like the library you're using may still use OAuth 1 instead of OAuth 2. – user94559 Sep 15 '15 at 17:16
  • Also, note that if your app is registered for the app folder permission, i.e., it's sandboxed as it seems to be based on your code `ul.Root = RootFolder.Sandbox;`, you don't need to manually provide paths like "/Apps/synch/". You should just provide "/" and so on, and it will be automatically translated into your app folder's path by the API. – Greg Sep 15 '15 at 20:15
  • Thanks @Greg I wish this to be completely automated. If this lib support oAuth1.0, can you point me to the library which uses oAuth2.0. As I get the access code from the app console, I wish to use it rather than me manually logging in. Thanks for your help – Timothy Rajan Sep 15 '15 at 20:18
  • For v1 of the Dropbox API, there isn't an official C# SDK, but there are third party ones, which may use OAuth 2: https://www.dropbox.com/developers/core/sdks/other For Dropbox API v2, currently just a preview, there is an official one: https://www.dropbox.com/developers-preview/documentation/c-sharp – Greg Sep 15 '15 at 20:29

1 Answers1

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);
Timothy Rajan
  • 1,947
  • 8
  • 38
  • 62