我尝试使用oneDrive REST API来获取特定文件夹中的文件。
我有文件夹的路径(例如" myApp / filesToDownload /",但是没有文件夹的oneDrive ID。有没有办法获取文件夹ID或使用REST API的文件夹中的文件?
我看到的唯一方法是使用https://apis.live.net/v5.0/me/skydrive/files?access_token=ACCESS_TOKEN 获取根目录中的文件夹列表,然后将路径字符串拆分为" /"并在其上循环,每次为每个层次结构执行GET https://apis.live.net/v5.0/CURRENT_FOLDER/files?access_token=ACCESS_TOKEN请求。我宁愿避免执行所有这些请求,因为路径可能很长..
是否有更好/更简单的方法来获取特定文件夹的文件?
由于
答案 0 :(得分:0)
正如Joel所指出的,Onedrive API也支持基于路径的寻址(除基于ID的寻址之外)。所以你不需要文件夹ID。您可以使用Onedrive API(api.onedrive.com)获取特定文件夹的文件/文件夹,如下所示:
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
}
有关详细信息,请参阅here。