Box: Create Shared Link to Folder
Create a URL to access the specified folder on Box. Created indefinitely if blank, Created without a password request if blank.
Configs
  • C1: OAuth2 Setting *
  • C2: Folder ID to share *#{EL}
  • C3: Data item with Expiration of the Link
  • C4: Data item with Password of the Link
  • C5: Data item that will save web the Shared Link *
Script (click to open)

main();
function main(){
  const folderId = configs.get("FolderId");

  if (folderId === "" || folderId === null) {
    throw "Folder ID is blank";
  }
  const dateItem = configs.get("unsharedAt");
  const passwordItem = configs.get("DownloadPass");
  
  let unsharedDate = null;
  if (dateItem !== "" && dateItem !== null){
    unsharedDate = engine.findDataByNumber(dateItem);
  }

  let password = "";
  if (passwordItem !== "" && passwordItem !== null){
    password = engine.findDataByNumber(passwordItem);
  }

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

  // フォルダが既に共有リンク作成されているか調べる
  const existingUrl = getFolderInformation(oauth2, folderId);

  //フォルダに既に共有リンクがある場合はエラーにする  共有リンクが無い場合は作成する
  if (existingUrl !== null){
    throw `Shared link(${existingUrl.url}) was already created.`;
  }
  const sharedlinkUrl = createSharedLink(oauth2, folderId, unsharedDate, password);

  const UrlData = configs.getObject( "DownloadUrlItem" );
  if (UrlData !== null) {
    engine.setData(UrlData,sharedlinkUrl);
  }
}

/**
  * Get Folder Information on Box  フォルダが既に共有リンク作成されているか調べる
  * @return {String}  フォルダの共有リンクオブジェクト
  */
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();
  engine.log(`status: ${status}`)

  if (status !== 200) {
    engine.log(responseTxt);
    throw "Failed to get folder information";
  }
  const jsonRes = JSON.parse(responseTxt);
  return jsonRes.shared_link;
}

/**
  * Create Shared Link to Folder on Box  共有リンク作成
  * @return {String}  フォルダの共有リンクURL
  */
function createSharedLink(oauth2, folderId, unsharedDate, password) {
  let timezone = engine.getTimeZoneId();
  if (timezone === "GMT"){
    timezone += "+00:00";
  }

  let jsonBody = {};

  jsonBody["shared_link"] = {"access": "open"};
  if(unsharedDate !== null){
    jsonBody["shared_link"]["unshared_at"] = unsharedDate.toString() + timezone.slice(3);
  }
  if(password !== "" && password !== null){
    jsonBody["shared_link"]["password"] = password;
  }
  
  jsonBody["shared_link"]["permissions"] = {"can_download": true };
  
  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();
  engine.log(`status: ${status}`)

  if (status !== 200) {
    engine.log(responseTxt);
    throw "Failed to create Shared Link";
  }
  const jsonRes = JSON.parse(responseTxt);
  return jsonRes.shared_link.url;
}

Download

2021-03-22 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/box-folder-link-create
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. You cannot recreate a shared link in a folder that already has a shared link.

Capture

See also

1 thought on “Box: Create Shared Link to Folder”

  1. Pingback: Box File Upload (Password / Expiration) – Questetra Support

Comments are closed.

%d bloggers like this: