Forum Discussion

saravananct143's avatar
saravananct143
Explorer | Level 3
8 years ago

How to List all files in the Dropbox folder using Web Request?


string accessToken = "Token";
var request = (HttpWebRequest)WebRequest.Create("https://api.dropboxapi.com/2/files/list_folder");

request.Method = "Post";
request.Headers.Add("Authorization", "Bearer " + accessToken);
request.ContentType = "application/json";


Stream newStream = request.GetRequestStream();

var response = (HttpWebResponse)request.GetResponse();
newStream.Close();

StreamReader sr = new StreamReader(response.GetResponseStream());
string responseString = sr.ReadToEnd();

 

I'm getting this error. How can i slove this ?

The remote server returned an error: (400) Bad Request.

7 Replies

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago
    Using the /2/files/list_folder endpoint is the right way to begin listing the contents of a folder.

    Please check the content of the response body. It should contain a more specific error message indicating what the issue is.
  • saravananct143's avatar
    saravananct143
    Explorer | Level 3
    8 years ago

    I'm trying this url in postman

    url: https://api.dropboxapi.com/2/files/list_folder

    Header : Authorization:Bearer Token

     

    This following error came . How can i solve this please help me

     

    Error in call to API function "files/list_folder": This function requires its argument in the HTTP request body, but your request body is empty

  • chirstius's avatar
    chirstius
    Icon for Dropbox Staff rankDropbox Staff
    8 years ago

    saravananct143 looking at your code above it seems like you are not adding any content to the body of your POST request.

     

    To do this for HttpWebRequest you will need to encode the data, and then add some additional code to send that data after you make the call to GetRequestStream().

    There is a good example of this in Microsoft's HttpWebRequest documentation found here:

    https://msdn.microsoft.com/en-us/library/d4cek6cc.aspx#Examples

    Note the creation and encoding of the postData variable into a byte array, and then, later on, the writing of that data to the request stream after the call to GetRequestStream().

     

    For your Dropbox API request, the postData content will be the JSON formatted parameters specified in the documentation for /list_folder that Greg-DB referenced earlier in this thread.

     

    Further, if you are using version 4.5 or newer of .NET, I would suggest looking at HttpClient vs HttpWebRequest as it provides a much simpler implementation for sending HTTP requests:

    https://msdn.microsoft.com/library/system.net.http.httpclient.aspx
     

    Hope that helps,

     

    -Chuck

     

     

  • saravananct143's avatar
    saravananct143
    Explorer | Level 3
    8 years ago
    Hi @Greak , please send any sample code for authendication token generation and retriving dropbox file.
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago
    We don't have official sample code for doing this in HttpWebRequest in particular. I recommend reviewing the resources Chuck shared above for help writing your code.
  • saravananct143's avatar
    saravananct143
    Explorer | Level 3
    8 years ago

    Without browser verification is it possible to create a authdication token?

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago
    No, the user needs to explicitly authorize the app in the browser in order for the app to receive an access token. The app can then store and re-use the resulting access token for future API calls though, without further manual user action.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!