Box: Create Shared Link to Folder
Creates a URL to access the specified folder on Box. If no expiration date is specified, the URL will be created indefinitely. If no password is specified, the URL will be created without requiring a password.
Configs: Common
  • Step Name
  • Note
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;
}

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)
  • You cannot recreate a shared link in a folder that already has a shared link.

Capture

See also

Appendix

  • box-folder-link-create.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: