upload file to dropbox with google app script using save_url from Dropbox API

房东的猫 提交于 2019-12-22 01:37:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!