Box: Delete Shared Link of File

Box: Delete Shared Link of File

Box: ファイル共有リンク削除

This item deletes a Shared Link URL of the specified file on Box. An error will occur if there is no shared link.

Auto Step icon
Basic Configs
Step Name
Note
Configs for this Auto Step
conf_OAuth2
C1: OAuth2 Setting *
conf_FileId
C2: File ID to delete Shared Link *

Notes

  • The file ID is contained in the URL: https://{sub-domain}.app.box.com/file/(File ID)
  • Box refresh tokens have an expiration date
  • If there is no shared link it will result in an error

Capture

See also

Script (click to open)
  • An XML file that contains the code below is available to download
    • box-file-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 auto step

function main() {
  const oauth2 = configs.getObject("conf_OAuth2");
  const fileId = decideFileId();
  checkExistingSharedLink(oauth2, fileId);
  deleteSharedLink(oauth2, fileId);
}

/**
 * ファイルのIDをconfigから読み出して出力する。
 * @return {String} fileId ファイルの ID
 */
function decideFileId() {
  let fileId = "";
  const fileIdDef = configs.getObject("conf_FileId");
  if (fileIdDef === null) {
    fileId = configs.get("conf_FileId");
  } else {
    fileId = engine.findData(fileIdDef);
  }
  if (fileId === "" || fileId === null) {
    throw "File ID is blank";
  }
  return fileId;
}

/**
 * ファイルに共有リンクが作成されているか調べ、無い場合はエラーにする
 * @param {AuthSettingWrapper} oauth2  OAuth2 認証設定
 * @param {String} fileId ファイルの ID
 */
function checkExistingSharedLink(oauth2, fileId) {
  const url = `https://api.box.com/2.0/files/${fileId}?fields=shared_link`;
  const response = httpClient.begin().authSetting(oauth2).get(url);

  const status = response.getStatusCode();
  const responseTxt = response.getResponseAsString();

  if (status !== 200) {
    engine.log(responseTxt);
    throw `Failed to get file information. status:${status}`;
  }
  const jsonRes = JSON.parse(responseTxt);

  if (jsonRes.shared_link === null) {
    throw `Failed to Delete Shared Link. \n The file(ID:${fileId}) doesn't have a Shared link.`;
  }
}

/**
 * Delete Shared Link to File on Box 共有リンク削除
 * @param {AuthSettingWrapper} oauth2  OAuth2 認証設定
 * @param {String} fileId 共有リンクを削除したいファイルのID
 */
function deleteSharedLink(oauth2, fileId) {
  const jsonBody = {};
  jsonBody["shared_link"] = null;

  const url = `https://api.box.com/2.0/files/${fileId}?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. status:${status}`;
    engine.log(responseTxt);
    throw error;
  }
  engine.log(`status: ${status} Shared Link was deleted, file ID: ${fileId}`);
}

1 thought on “Box: Delete Shared Link of File”

  1. Pingback: Box: Create Shared Link to File – Questetra Support

Comments are closed.

Scroll to Top

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading