Box: Delete Shared Link of Folder
Delete a Shared Link URL of the specified folder on Box. No shared link will occur an error.
Configs
  • C1: OAuth2 Setting *
  • C2: Folder ID to delete Shared Link *
Script (click to open)

main();
function main(){

const oauth2 = configs.get("OAuth2");

let folderId = decideFolderId();

// フォルダに共有リンクがあるか調べる
const existingUrl = getFolderInformation(oauth2, folderId);

// フォルダに共有リンクが無い場合はエラーにする 共有リンクがある場合は削除する
if (existingUrl === null){
const error = `Failed to Delete Shared Link \n The folder(ID:${folderId}) doesn't have a Shared link.`;
throw error;
}
deleteSharedLink(oauth2, folderId);
}

/**
* フォルダのIDをconfigから読み出して出力する。
* @return {String} folderId フォルダの ID
*/
function decideFolderId(){
let folderId = "";
const folderIdDef = configs.getObject("conf_FolderId");
if(folderIdDef === null){
folderId = configs.get( "conf_FolderId");
}else{
folderId = engine.findData(folderIdDef);
}

if (folderId === "" ||folderId === null) {
throw "Folder ID is blank";
}
if(folderId === "0") {
throw "Root folder is not eligible.";
}
return folderId;
}



/**
* Get Folder Information on Box フォルダに共有リンクがあるか調べる
* @param {String} oauth2 認証設定
* @param {String} folderId 調べたいフォルダのID
* @return {Object} jsonRes.shared_link フォルダの共有リンクオブジェクト
*/
function getFolderInformation(oauth2, folderId) {

const url = "https://api.box.com/2.0/folders/" + folderId + "?fields=shared_link";
const response = httpClient.begin()
.authSetting(oauth2)
.get(url);
const status = response.getStatusCode();
const responseTxt = response.getResponseAsString();
if (status !== 200) {
const error = `Failed to get folder information \n status: ${status}`;
engine.log(responseTxt);
throw error;
}

const jsonRes = JSON.parse(responseTxt);

return jsonRes.shared_link;
}




/**
* Delete Shared Link to Folder on Box 共有リンク削除
* @param {String} OAuth2 認証設定
* @param {String} folderId 共有リンクを削除したいフォルダのID
*/
function deleteSharedLink(oauth2, folderId) {

let jsonBody = {};

jsonBody["shared_link"] = null;

const url = "https://api.box.com/2.0/folders/" + folderId + "?fields=shared_link";
const response = httpClient.begin()
.authSetting(oauth2)
.body(JSON.stringify(jsonBody), "application/json; charset=UTF-8")
.put(url);
const status = response.getStatusCode();
const responseTxt = response.getResponseAsString();
if (status !== 200) {
const error = `Failed to delete \n status: ${status}`;
engine.log(responseTxt);
throw error;
}
engine.log(`status: ${status} Shared Link was deleted, forder ID: ${folderId}`);
}


Download

2021-01-25 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/bpmn-icons/service-task-box-folder-link-delete/
The Addon-import feature is available with Professional or Enterprise edition.

Notes

  1. The folder ID is contained in the URL:  https://{sub-domain}.app.box.com/folder/(Folder ID)
  2. Box refresh tokens have an expiration date. Use it regularly to ensure that it does not exceed the expiration. (60 days, as of July 2019. https://box-content.readme.io/docs/oauth-20)
  3. No shared link will occur an error

Capture

See also

%d bloggers like this: