Box: Delete Shared Link of Folder
Deletes the shared link URL of a specified folder on Box. If there is no shared link an error will occur.
Configs: Common
  • Step Name
  • Note
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}`);
}


Notes

  • The folder ID is contained in the URL: https://{sub-domain}.app.box.com/folder/(Folder ID)
  • Box refresh tokens have an expiration date. Use it regularly to ensure that it does not exceed the expiration. (Box: Token & URL Expiration)
  • If there is no shared link it will result in an error

Capture

See also

Appendix

  • box-folder-link-delete.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on
%d bloggers like this: