We're making changes to the Community, so you may have received some notifications - thanks for your patience and welcome back. Learn more here.

Forum Discussion

malark's avatar
malark
Helpful | Level 5
8 years ago

Examples for list folder using dropbox java api v2

Hi, I am trying to use dropbox client api v2 with java HttpURLConnection to list folders in dropbox. The below example did not work. It returns 400 Bad request. Can you please help me ?

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class DBClientService {

    private static final String token = "Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
   public static void main(String[] args) {
        
      try {

        URL url = new URL("https://api.dropboxapi.com/2/files/list_folder");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Accept", "application/json");        
        conn.addRequestProperty ("Authorization", token);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.addRequestProperty("path", "/test_java_createfolder");
        conn.addRequestProperty("recursive", "false");
        conn.addRequestProperty("include_media_info", "false");
        conn.addRequestProperty("include_deleted", "false");
        conn.addRequestProperty("include_has_explicit_shared_members", "false");
        
        if (conn.getResponseCode() != 200) {
            System.out.println(conn.getResponseMessage());
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        conn.disconnect();

      } catch (MalformedURLException e) {

        e.printStackTrace();

      } catch (IOException e) {

        e.printStackTrace();

      }

    }

}

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,875 PostsLatest Activity: 58 minutes ago
325 Following

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 or Facebook.

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!