使用Dropbox API中的save_url将文件上传到带有google应用脚本的Dropbox

时间:2017-09-12 18:58:49

标签: google-apps-script dropbox-api

我正在尝试在Google应用脚本中实现所述内容here。但我没有足够的经验,所以看看有什么不对。所以任何帮助将不胜感激。

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);
  }
}

我收到以下错误消息:

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 个答案:

答案 0 :(得分:1)

我找到了一个有效的解决方案:

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;  
}