Search code examples
google-apps-scriptdropbox-apidrive

send files from google drive to dropbox with google app scripts


I've been searching how to do this, but i didn't find anything. After a while, i came up with the script i'll post as an answer. I'm posting this for myself and anyone who might find it useful.

You'll need to get a dropbox access token, that can be obtained after creating a dropbox app.


Solution

  • 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: JSON.stringify(payload)    
      }; 
    
      var response = UrlFetchApp.fetch(dropboxurl, options);  
      return response;  
    }
    

    You can find an example HERE