cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Whether you are transferring a single drive, a team or an entire organization, Movebot's cloud migration tool has been built to make your Dropbox migration simple - learn all about it here.

Dropbox installs & integrations

Connect your tools and content together with help from the Dropbox Community. Join a discussion or post a question of your own to get started.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

HTTP Request in C# using WebClient

HTTP Request in C# using WebClient

mpalmer-sps
Explorer | Level 3

I'm trying to get the contents of a folder for downloading from Dropbox in C# using the HTTP libraries. So far I've been trying to use a console application project in Visual Studio but don't seem to be making alot of headway.

What I would like to do is A) See if there are files in the folders and then B) download then to a location (in this case a SharePoint library but I can't seem to make the initial connection using .Net 3.5

Code sample so far:

        static void Main(string[] args)
        {

            using (WebClient client = new WebClient())
            {
                var URI = new Uri("https://api.dropboxapi.com/2/files/list_folder");

                client.Headers.Set("Content-Type", "application/json");
                client.Headers.Add("Authorization", "Bearer <MyApplicationAccessToken>");

                // data
                var reqparm = new System.Collections.Specialized.NameValueCollection();
                reqparm.Add("path", "/<MyApplicationFolder>/");
                reqparm.Add("recursive", "false");
                reqparm.Add("include_media_info", "false");
                reqparm.Add("include_deleted", "false");
                reqparm.Add("include_has_explicit_shared_members", "false");
                reqparm.Add("include_mounted_folders", "true");
                byte[] reqBytes = Encoding.ASCII.GetBytes(reqparm.ToString());

                byte[] responsebytes = client.UploadData(URI, "POST", reqBytes);
                string responsebody = Encoding.UTF8.GetString(responsebytes);
            }
        }

Any ideas? Cant use the .Net SDK

6 Replies 6

Re: HTTP Request in C# using WebClient

Greg-DB
Dropboxer

To list files in a folder, you should use /2/files/list_folder and /2/files/list_folder/continue. To download files, you should use /2/files/download.

 

If an API call fails, the response will contain an error. What response body are you getting back?

Re: HTTP Request in C# using WebClient

mpalmer-sps
Explorer | Level 3

Thanks Greg, I'm getting a 400 Error (Bad Request) . However, I'm able to use the same parameters and headers in RestMan via Chrome but I don't "fool" the Server into thinking this is coming from a Browser.

 

Is there a functional code sample using C# and HTTP for DropBox?

 

Re: HTTP Request in C# using WebClient

mpalmer-sps
Explorer | Level 3

RestMan setupRestMan setup

Above is a Restman call that gives me a cursor - so I'm trying to replicate that request in code.

Re: HTTP Request in C# using WebClient

Greg-DB
Dropboxer
We recommend just using the official .NET SDK, so we don't have a sample for doing this directly.

Anyway, the error information you shared is just the status code/line. The response body should contain a more useful error message. Checking the response body is generally the best first step in debugging these. Can you print that out? Thanks in advance!

Re: HTTP Request in C# using WebClient

mpalmer-sps
Explorer | Level 3

It looks like the parameters I'm sending are not in JSON format (see below):

joson.png

Re: HTTP Request in C# using WebClient

Greg-DB
Dropboxer
Thanks! That's correct, that error message indicates that the request body (containing the API call parameters) wasn't valid JSON as required.

Looking at your code, that seems likely, as I don't see where you're building it as JSON, and the code you're calling presumably doesn't default to returning JSON.

You should update your code to take your parameters and format them as a JSON string for use in the request body. You'll probably want to use a JSON library for that, such as Json.NET.
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropboxer
  • User avatar
    mpalmer-sps Explorer | Level 3
What do Dropbox user levels mean?
Need more support?