Google Drive: Create Permission
Create Permissions in the specified files on Google Drive
https://support.questetra.com/addons/googledrive-permissioncreate/
2018-12-07 (C) Questetra, Inc. (MIT License)
Configs
  • C1.User who connect to Google Drive *
  • I1. File / Folder Ids to share *
  • C2. Can find or not (true or false) *
  • C3. Can edit or not (true or false) *
Script
main();

function main() {
  let fileIds = engine.findDataByNumber(configs.get("FileIdItem"));
  if (fileIds == null) {
    throw "ID isn't set.";
  }
  fileIds += "";
  
  const userId = configs.get("UserID");
  const quser = quserDao.findById(parseInt(userId));
  if (quser == null) {
    throw "User not found:" + userId;
  }
  
  //var sharedRange = readChoicesFromConfig("SharedRange", ["public", "domain"]);
  let sharedRange = "public";
  let allowFileDiscovery = readChoicesFromConfig("AllowFileDiscovery", ["true", "false"]);
  let editable = readChoicesFromConfig("Editable", ["true", "false"]);
  
  // get OAuth token
  let token;
  try{
    token = httpClient.getGoogleOAuth2Token(quser, "Drive");
  }catch(e){
    throw "This User has not connected with Google Drive."
  }
  
  fileIds.split("\n").forEach(function(fileId){
    createPermission(token, fileId, sharedRange, allowFileDiscovery, editable);
  });
}

// read config. throw error when config value is not available value.
function readChoicesFromConfig(configName, choices) {
  let value = configs.get(configName);
  for (let i=0; i<choices.length; i++) {
    if (choices[i] == value) {
      return value;
    }
  }
  throw "Config " + configName + " is invalid: " + value;
}

// create permission on Google Drive
function createPermission(token, fileId, shareRange, allowFileDiscovery, editable) {
  let json = {};
  json["role"] = editable == "true" ? "writer" : "reader";
  json["type"] = shareRange == "public" ? "anyone" : "domain";
  json["allowFileDiscovery"] = allowFileDiscovery + '';
  
  const url = 'https://www.googleapis.com/drive/v3/files/' + fileId + '/permissions';
  let response = httpClient.begin()
    .bearer(token)
    .queryParam("supportsTeamDrives","true")
    .body(JSON.stringify(json), "application/json; charset=UTF-8")
    .post(url);
  var status = response.getStatusCode();
  if (status >= 300) {
    const error = "Failed to create permission:" + fileId + "\n" + status + "\n" + response.getResponseAsString();
    throw error;
  }
}

Download

google-drive-permission-create.xml

Capture

Notes

  1. Users of C1 need to have configured connection with Google Drive at [Account Settings] > [Google Connectivity]