Skip to main content
Kinetic Community

Tutorial: Setting up Expiring Links in Google Drive

The Google Drive Bridge supports retrieving a file with an expiring link. To make this feature work, you will need to set up Google Apps Script inside of the Google Drive account which you will be reading the contents from.

Copying the Google Apps Script to your Drive account

A copy of the script is here:

// Script-as-app template.

function doGet(e) {
  var testId = "197G61XR6mUx4lGZ2e9QXmY6UDberEyJqXrHV9IY8ig8"
  var fileId = e.parameter.fileId;
  
  var today = new Date();
  var expFolder = DriveApp.createFolder("expiringLink" + today.getTime().toString());
  var file = DriveApp.getFileById(fileId);
  var fileCopy = file.makeCopy();
  expFolder.addFile(fileCopy);
  
  var json = null;
  if (PropertiesService.getScriptProperties().getProperty("jsonProps") !== null) {
    var properties = PropertiesService.getScriptProperties().getProperty("jsonProps");
    json = JSON.parse(properties);
  } else {
    json = {};
    json["expiringArray"] = [];
  }
  
  json["expiringArray"].push(expFolder.getId()); 
  var jsonString = JSON.stringify(json);
  PropertiesService.getScriptProperties().setProperty("jsonProps",jsonString);
  Logger.log(jsonString);

  // Set the folder to delete after 30 minutes
  var deleteDelay = 30;
  expireDate = addMinutes(today, deleteDelay);
  ScriptApp.newTrigger("deleteFile").timeBased().at(expireDate).create();
  
  var output = {}
  output["id"] = fileCopy.getId();
  output["url"] = fileCopy.getUrl();
  
  return ContentService.createTextOutput(JSON.stringify(output));
}


function deleteFile() {
  var properties = PropertiesService.getScriptProperties().getProperty("jsonProps");
  var json = JSON.parse(properties);
  Logger.log(json);
  var folderId = json["expiringArray"].shift();
  
  var folder= DriveApp.getFolderById(folderId);
  folder.setTrashed(true);
  
  var jsonString = JSON.stringify(json);
  PropertiesService.getScriptProperties().setProperty("jsonProps",jsonString);
  Logger.log(jsonString);
}

function addMinutes(date, minutes) {
    return new Date(date.getTime() + minutes*60000);
}

Next, you want to make your own copy of it to have in your account. The contents of the script are also included in a .txt file attached to this page. Just take the contents of the file and copy them into a Google Apps Script and save it to your drive.

Deploy the Script as a Web App

To be able to access the script from the Bridge, you will need to deploy the script as a web app. To do that, select Public > Deploy as Web App. Give a description to the Project Version (for example, Initial Version) and save it. Then, make sure that the Execute the app as is set to me. Lastly, set Who has access to the app to Anyone, even anonymous. Just click Deploy to finish the process.

Copy the Url and Authorize the Web App

Copy the url that pops up after successfully deploying the web app. This will be used to be put into the Bridge Properties for the Google Drive Bridge. Authorizing the web app is the last thing that needs to happen before being able to use it to create an expiring link. To authorize the app, select Run > addMinutes. When the Authorization required dialog box pops up, click continue and follow the steps to finish authorization. The web app is now ready to go and can be used in conjunction with the Google Drive Bridge to make expiring links for your files!