Last active
May 12, 2021 06:38
-
-
Save lqez/ba5d0ddec168958bd69e17a359b98439 to your computer and use it in GitHub Desktop.
An example of using Dropbox API in Google Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var token = '<PUT_YOUR_TOKEN_HERE>'; | |
function dropboxAPI(path, data) { | |
var response = UrlFetchApp.fetch('https://api.dropboxapi.com/2/' + path, { | |
'method': 'POST', | |
'payload': JSON.stringify(data), | |
'headers': { | |
'Authorization': 'Bearer ' + token, | |
'Content-Type': 'application/json' | |
}, | |
'muteHttpExceptions': true | |
}); | |
if (parseInt(response.getResponseCode()) != 200) { | |
return undefined; | |
} | |
return JSON.parse(response.getContentText()); | |
} | |
function getFileByName(name) { | |
var result = dropboxAPI('/files/search', { | |
'path': '', | |
'query': name, | |
'start': 0, | |
'max_results': 1, | |
'mode': 'filename', | |
}); | |
if (result) { | |
return result.matches[0].metadata; | |
} | |
return undefined; | |
} | |
function getURLFromDropbox(name) { | |
file = getFileByName(name); | |
if (file === undefined) { | |
return ''; | |
} | |
var result = dropboxAPI('/files/get_temporary_link', { | |
'path': file.path_lower, | |
}); | |
if (result) { | |
Logger.log('Successfully get a link:', result.link); | |
return result.link; | |
} | |
return ''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using in Google SpreadSheet:
Screenshot:
