Forum Discussion

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

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();

      }

    }

}