I have a script (proposed by @Tanaike) that gets file details from a specific folder of Dropbox. Here is the code snippet:
function listDropBoxFiles() {
const accessToken = "###"; // Please set your access token.
const path = "/Master Folder";
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 = [];
var fileDetails = [];
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);
for (var i = 0; i < values.length; i++) {
var entry = values[i];
// Check if the entry is a file
if (entry['.tag'] === 'file') {
var fileUrl = getFileUrl(accessToken,entry.id)
fileDetails.push([entry.name,fileUrl,Utilities.formatDate(new Date(entry.client_modified), Session.getScriptTimeZone(), 'dd/MM/yyyy hh:mm a'),Utilities.formatDate(new Date(entry.server_modified), Session.getScriptTimeZone(), 'dd/MM/yyyy hh:mm a')])
}
}
console.log(fileDetails);
detailSheet.getRange(detailSheet.getLastRow()+1,1,fileDetails.length,fileDetails[0].length).setValues(fileDetails);
}
function getFileUrl(accessToken,fileId) {
const url = "https://api.dropboxapi.com/2/sharing/get_file_metadata";
const res = UrlFetchApp.fetch(url, {
method: "post",
headers: { authorization: "Bearer " + accessToken },
contentType: "application/json",
payload: JSON.stringify({ file: fileId })
});
const obj = JSON.parse(res.getContentText());
console.log(obj);
const sharedLink = obj.preview_url;
return sharedLink;
}
It generates output
in an array like this:
[[ 'Julian Sheets.txt', 'https://www.dropbox.com/scl/fi/x243qx9apv27gugiege89/Julian-Sheets.txt?dl=0',
'11/09/2023 11:51 AM',
'11/09/2023 01:27 PM' ]]
But the issue is this file link is not publicly accessible. When you click on it, it asks you to sign in to Dropbox in order to see this file. Is there any way that we can get a public link that does not require sign-in?
In your script, how about the following modification? In this case, please modify your function getFileUrl(accessToken,fileId)
as follows.
function getFileUrl(accessToken, fileId) {
const url1 = "https://api.dropboxapi.com/2/sharing/list_shared_links";
const res1 = UrlFetchApp.fetch(url1, {
method: "post",
headers: { authorization: "Bearer " + accessToken },
contentType: "application/json",
payload: JSON.stringify({ path: fileId })
});
const obj1 = JSON.parse(res1.getContentText());
if (obj1.links.length > 0) {
return obj1.links[0].url;
}
const url2 = "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings";
const res2 = UrlFetchApp.fetch(url2, {
method: "post",
headers: { authorization: "Bearer " + accessToken },
contentType: "application/json",
payload: JSON.stringify({ path: fileId, settings: { access: "viewer", allow_download: true, audience: "public", requested_visibility: "public" } })
});
const obj2 = JSON.parse(res2.getContentText());
console.log(obj2);
const sharedLink = obj2.url;
return sharedLink;
}
By this modification, the shared URLs are included in the output value. When you access the shared URL, you can see the file without logging in to Dropbox.
About the request body, please confirm the official document of "/create_shared_link_with_settings".