Download Web Files to Dropbox with Google Apps Script

D
Published in: Google Apps Script - Dropbox

Use Google Apps Script with the Dropbox API to download any file from the Internet and upload it directly to the Internet without saving it to your computer. You need to specify the URL of the web file and path in your Dropbox folder where that file should be saved. If a file in the given path already exists, the new file will be renamed.

To get started, sign-in to your Dropbox account and create a new Dropbox app as shown in the screenshot below.

Dropbox API

Go to the next Dropbox screen and create an access token. By generating an access token, you will be able to make Dropbox API calls for your own account without going through the Dropbox OAuth2 authorization flow. To obtain access tokens for other users, use the standard OAuth flow.

function saveWebFilesToDropbox(fileUrl) {

  var accessToken = 'Dropbox-xxxx-1234';
  var headers = {
    "Content-Type": "application/json",
    'Authorization': 'Bearer ' + accessToken
  };

  fileUrl = fileUrl || "https://img.labnol.org/files/Most-Useful-Websites.pdf";
  var parameters = {
    "url": fileUrl,
    "path": "/PDF/book.pdf"
  };

  var options = {
    "method": "POST",
    "headers": headers,
    "payload": JSON.stringify(parameters)
  };

  var apiUrl = "https://api.dropboxapi.com/2/files/save_url";
  var response = JSON.parse(UrlFetchApp.fetch(apiUrl, options).getContentText());

  var checkUrl = apiUrl + "/check_job_status";
  var checkOptions = {
    "method": "POST",
    "headers": headers,
    "payload": JSON.stringify({
      "async_job_id": response.async_job_id
    })
  };

  do {
    Utilities.sleep(1000);
    response = JSON.parse(UrlFetchApp.fetch(checkUrl, checkOptions).getContentText());
  } while (response[".tag"] != "complete");

  Logger.log("File uploaded successfully to Dropbox");

}

The /save_url endpoint saves the file at the specified URL in your Dropbox. It returns a Job ID since the upload process is asynchronous. You can make calls to /save_url/check_job_status to check the upload status when the return code is “complete”, the file has been successfully uploaded to your Dropbox folder.

The SaveUrl functionality in Dropbox doesn’t have a file size limit, but the download operations on the Dropbox servers do time out after 5 minutes. So, if it takes longer than 5 minutes to transfer the file from the URL to the Dropbox servers, the file won’t be saved.

If you call /save_url/check_job_status again to check later (e.g., after at most 5 minutes) it should return either information about the successfully saved file, or an error message indicating the issue.

📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script - Dropbox

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube