This is a continuation of my previous question for which @Tanaike proposed a very good solution to get files from a specific folder. However, it needs modification if there are subfolders in a specific folder. Here is sample script:
function myFunction() {
const accessToken = "###"; // Please set your access token.
const path = "/SampleName";
const url = "https://api.dropboxapi.com/2/files/list_folder";
const res = UrlFetchApp.fetch(url, {
method: "post",
headers: { authorization: "Bearer " + accessToken },
contentType: "application/json",
payload: JSON.stringify({ path, limit: 2000, recursive: true })
});
const obj = JSON.parse(res.getContentText());
console.log(obj);
}
The endpoint that will be used to get files from subfolder of a folder is:
https://api.dropboxapi.com/2/files/list_folder/continue
However, due to my basic skills, I need guidance to use this in the above script. Thank you in advance.
In your situation, how about the following modification?
function myFunction() {
const accessToken = "###"; // Please set your access token.
const path = "/SampleName";
let url = "https://api.dropboxapi.com/2/files/list_folder";
let has_more = false;
const option = {
method: "post",
headers: { authorization: "Bearer " + accessToken },
contentType: "application/json",
payload: JSON.stringify({ path, limit: 2000, recursive: true })
};
let values = [];
do {
const res = UrlFetchApp.fetch(url, option);
let obj = JSON.parse(res.getContentText());
values = [...values, ...obj.entries];
has_more = obj.has_more;
if (has_more) {
url = "https://api.dropboxapi.com/2/files/list_folder/continue";
option.payload = JSON.stringify({ cursor: obj.cursor });
}
} while (has_more);
console.log(values);
}
has_more
is true, the list is retrieved from the next page using cursor
.