Box: ファイル共有リンク削除 (Box: Delete Shared Link of File)
この工程は、Box 上の指定ファイルの共有リンク URL を削除します。共有リンクが無い場合は、エラーとなります。
Configs:共通設定
  • 工程名
  • メモ
Configs
  • C1: OAuth2 設定 *
  • C2: 共有リンクを削除するファイルの ID *

Notes

  • ファイルID は、URL に含まれています https://{sub-domain}.app.box.com/file/(File ID)
  • Box のリフレッシュトークンには、期限があります
  • 共有リンクが無い場合は、エラーとなります

Capture

See also

Script (click to open)
  • 下記のスクリプトを記述した XML ファイルをダウンロードできます
    • box-file-link-delete.xml (C) Questetra, Inc. (MIT License)
    • Professional をご利用であればファイルの内容を改変することでオリジナルのアドオンとして活用できます

main();
function main() {
  const oauth2 = configs.get("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 {String} oauth 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 {String} 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}`);
}
%d人のブロガーが「いいね」をつけました。