Dropbox api to download file using c#

Almost most of the people use the dropbox to store and share files. But sometime we need to access the dropbox files.

Recently I have requirement to download the dropbox files using and c#. I have searched on google regarding the dropbox api and get in complete details. As dropbox doesn’t support c# api. I found some third party libraries to access dropbox and able to download the respective files.

I used DropboxRestAPI to download files from the dropbox. Before use dropbox rest api you have to follow some initial steps like :-

1. Create Dropbox Console App. Create dropbox console app.
2. Once you create console app, you will be able to get App key, Secret key, and Access Token
3. Now create a new project and add below code and change your app key, secret key and access token.
4. One more thing don’t forgot to addDropboxRestAPI using nuget, if uisng asp.net with c#

 var options = new Options
               ClientId = "AppKey", //get from dropbox app console
               ClientSecret = "AppSecret", //get from dropbox app console
               AccessToken = "AccessToken",  //get from dropbox app console
               RedirectUri = "https://api.dropbox.com/1/oauth/request_token"

           // Initialize a new Client (without an AccessToken)
           var client = new Client(options);
           // Get the OAuth Request Url
           var authRequestUrl = client.Core.OAuth2.AuthorizeAsync("code");

           // TODO: Navigate to authRequestUrl using the browser, and retrieve the Authorization Code from the response
           var authCode = "...";

           // Exchange the Authorization Code with Access/Refresh tokens
           var token =  client.Core.OAuth2.TokenAsync(authCode);

           // Get root folder without content
           var rootFolder =  client.Core.Metadata.MetadataAsync("/", list: false);
           Console.WriteLine("Root Folder: {0} (Id: {1})", rootFolder.Result.Name, rootFolder.Result.path);

           // Get root folder with content
           rootFolder = client.Core.Metadata.MetadataAsync("/", list: true);
           foreach (var folder in rootFolder.Result.contents)
               Console.WriteLine(" -> {0}: {1} (Id: {2})",
                   folder.is_dir ? "Folder" : "File", folder.Name, folder.path);

           // Find a file in the root folder
           var file = rootFolder.Result.contents.FirstOrDefault(x=>x.is_dir==false);
            // Download a file
            var tempFile = @"D:\"+file.Name;
           using (var fileStream = System.IO.File.OpenWrite(tempFile))
               client.Core.Metadata.FilesAsync(file.path, fileStream);

It’s very easy to work with dropboxrestapi, if you guys still face any problem. Please hire me.

Happy coding…..

Posted by | View Post | View Group