问题
I'm trying to implement what is described here in a google app script. But i don't have enough experience so see what is wrong. So any help will be appreciated.
function Gmail2Dropbox() {
var dropboxTOKEN = 'XXXxxx';
var folderName = "Folder";
var folder = DriveApp.getFoldersByName(folderName).next();
var files = folder.getFiles();
while (files.hasNext()){
var file = files.next();
var fileName = file.getName();
var fileid = file.getId();
var path = 'path/'+fileName;
var dropboxurl = 'https://api.dropboxapi.com/1/save_url/auto/'+path;
var fileurl = 'https://drive.google.com/uc?export=download&id=' + fileid;
Logger.log(dropboxurl);
var options = {
"Authorization": 'Bearer '+ dropboxTOKEN,
"method" : 'POST',
"url" : fileurl
};
var response = UrlFetchApp.fetch(dropboxurl, options);
Logger.log(response);
}
}
I get the following error message:
Request failed for https://api.dropboxapi.com/1/save_url/auto/path returned code 400.
Truncated server response: {"error_description": "No auth function available for given request",
"error": "invalid_request"} (use muteHttpExceptions option to examine full response). (line 25, file "Gmail2Dropbox")
回答1:
I got a working solution:
function Gmail2Dropbox() {
var folderName = "Folder";
var folder = DriveApp.getFoldersByName(folderName).next();
var files = folder.getFiles();
while (files.hasNext()){
var file = files.next();
var response = send2dropbox(file);
Logger.log(response);
}
}
function send2dropbox(file) {
var dropboxTOKEN = 'XXXxxx';
var path = 'somePath/' + file.getName();
var dropboxurl = 'https://api.dropboxapi.com/2/files/save_url';
var fileurl = 'https://drive.google.com/uc?export=download&id=' + file.getId();
var headers = {
'Authorization': 'Bearer ' + dropboxTOKEN,
'Content-Type': 'application/json'
};
var payload = {
"path": path,
"url": fileurl
}
var options = {
method: 'POST',
headers: headers,
payload: payload
};
var response = UrlFetchApp.fetch(dropboxurl, options);
return response;
}
来源:https://stackoverflow.com/questions/46183615/upload-file-to-dropbox-with-google-app-script-using-save-url-from-dropbox-api