2
votes


I'm trying to use the oneDrive REST API to get files in a specific folder. I have the path to the folder (For example "myApp/filesToDownload/", but don't have the folder's oneDrive ID. Is there a way to get the folder ID or the files in the folder with the REST API?

The only way I see to get it is by using https://apis.live.net/v5.0/me/skydrive/files?access_token=ACCESS_TOKEN to get the list of folders in the root, and then splitting the path string on "/" and looping on it, each time doing a GET https://apis.live.net/v5.0/CURRENT_FOLDER/files?access_token=ACCESS_TOKEN request for each hierarchy.. I would prefer to avoid doing all those requests because the path may be quite long..

Is there a better/simpler way of getting the files of a specific folder?

Thanks

1
The OneDrive API (api.onedrive.com) supports path based addressing. See dev.onedrive.com/misc/addressing.htm for more details. Edit: Depending on your scenario, you may want to look into special folders - specifically the approot special folder, which is documented at dev.onedrive.com/misc/appfolder.htmJoel

1 Answers

0
votes

As Joel has pointed out, Onedrive API supports path based addressing also (in addition to ID based addressing). So you don't need the folder ID. You can use the Onedrive API (api.onedrive.com) for getting the files/folders of a specific folder as follows:

        String path = "path/to/your/folder"; // no '/' in the end
        HttpClient httpClient = new DefaultHttpClient();

        // Forming the request
        HttpGet httpGet = new HttpGet("https://api.onedrive.com/v1.0/drive/root:/" + path + ":/?expand=children");
        httpGet.addHeader("Authorization", "Bearer " + ACCESS_TOKEN);

        // Executing the request
        HttpResponse response = httpClient.execute(httpGet);

        // Handling the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        StringBuilder builder = new StringBuilder();
        for (String line = null; (line = reader.readLine()) != null;) {
            builder.append(line).append("\n");
        }

        JSONTokener tokener = new JSONTokener(builder.toString());
        JSONObject finalResult = new JSONObject(tokener);

        JSONArray fileList = null;
        try{
            fileList = finalResult.getJSONArray("children");

            for (int i = 0; i < fileList.length(); i++) {
                JSONObject element = (JSONObject) fileList.get(i);

                // do something with element 
                // Each element is a file/folder in the form of JSONObject 
            }
        } catch (JSONException e){
            // do something with the exception
        }

For more details see here.