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;
}
}
Pingback: Utilizing Google Drive from Workflow – Sending files to people outside the company – Questetra Support