Search code examples
jspdropboxfilelist

How to get fileList by Dropbox?


I have a question. but I do not speak English well. Please understand.

I got the access token from the Dropbox. and I want a list of files.

I could not find the Dropbox Core API. Please tell us how to get a list of files.

======================================================================

gpgekko// I've been told you have to find metadata does not have the desired result.

This URL is for importing metadata.

https://api.dropbox.com/1/metadata/<root>/<path>

/<root>/<path> exclusion did not know what it is.

JSP file, this function is called.

public void getFiles() throws IOException {
    consoleCheck("getFiles");

    String parameters = "access_token=" + access_token;
    parameters += "&redirect_uri=" + redirect_uri;
    parameters += "&list=true";
    URL url = new URL("https://api.dropbox.com/1/metadata/");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    DataOutputStream writer = new DataOutputStream(conn.getOutputStream());
    writer.writeBytes(parameters);
    writer.flush();

    if (writer != null)
        writer.close();

    InputStream inputStream = conn.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    String line;
    StringBuffer stringBuffer = new StringBuffer();

    while ((line = reader.readLine()) != null) {
        stringBuffer.append(line);
        stringBuffer.append('\r');
    }
    JSONObject object = (JSONObject) JSONValue.parse(stringBuffer.toString());
    System.out.println("object => " + object);
}

Solution

  • Solved in this way.

    I remove redirect_uri in parameter and add /dropbox/ in end of url

    public JSONObject getFiles() throws IOException {
        consoleCheck("getFiles");
    
        String parameters = "access_token=" + access_token;
        //parameters += "&redirect_uri=" + redirect_uri;
        parameters += "&list=true";
        URL url = new URL("https://api.dropbox.com/1/metadata/dropbox/");
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
    
        DataOutputStream writer = new DataOutputStream(conn.getOutputStream());
        writer.writeBytes(parameters);
        writer.flush();
    
        if (writer != null)
            writer.close();
    
        InputStream inputStream = conn.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        StringBuffer stringBuffer = new StringBuffer();
    
        while ((line = reader.readLine()) != null) {
            stringBuffer.append(line);
            stringBuffer.append('\r');
        }
        JSONObject object = (JSONObject) JSONValue.parse(stringBuffer.toString());
        //System.out.println("object => " + object);
        return object;
    }
    

    result type is JSONObejct. and result value is like this.

    {
    "size": "0 bytes",
    "hash": "37eb1ba1849d4b0fb0b28caf7ef3af52",
    "bytes": 0,
    "thumb_exists": false,
    "rev": "714f029684fe",
    "modified": "Wed, 27 Apr 2011 22:18:51 +0000",
    "path": "/Public",
    "is_dir": true,
    "icon": "folder_public",
    "root": "dropbox",
    "contents": [
        {
            "size": "0 bytes",
            "rev": "35c1f029684fe",
            "thumb_exists": false,
            "bytes": 0,
            "modified": "Mon, 18 Jul 2011 20:13:43 +0000",
            "client_mtime": "Wed, 20 Apr 2011 16:20:19 +0000",
            "path": "/Public/latest.txt",
            "is_dir": false,
            "icon": "page_white_text",
            "root": "dropbox",
            "mime_type": "text/plain",
            "revision": 220191
        }
    ],
    "revision": 29007
    }
    

    thanks all.