Box: Create Shared Link to Folder
Creates a URL to access the specified folder on Box. If no expiration date is specified it is created indefinitely. If no password is specified the URL will be created without a password requirement.
2020-10-20 (C) Questetra, Inc. (MIT License)
Configs
  • C1: OAuth2 Setting Name *
  • C2: Folder ID to share * #{EL}
  • C3: Datetime type data item with Expiration of the Link
  • C4: String type data item with Password of the Link
  • C5: String type data item that will save web the Shared Link *
Script
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 OAuth token
  const token = httpClient.getOAuth2Token(configs.get("OAuth2"));

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

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

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

/**
  * Get Folder Information on Box  フォルダが既に共有リンク作成されているか調べる
  * @return {String}  フォルダの共有リンクオブジェクト
  */
function getFolderInformation(token, folderId) {

  const url = "https://api.box.com/2.0/folders/" + folderId + "?fields=shared_link";
  const response = httpClient.begin()
    .bearer(token)
    .get(url);
  const status = response.getStatusCode();
  const responseTxt = response.getResponseAsString();
  if (status !== 200) {
    engine.log(responseTxt);
    const error = `Failed to get folder information \n status: ${status}`;
    throw error;
  }

  const jsonRes = JSON.parse(responseTxt);

  return jsonRes.shared_link;
}



/**
  * Create Shared Link to Folder on Box  共有リンク作成
  * @return {String}  フォルダの共有リンクURL
  */
function createSharedLink(token, 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()
    .bearer(token)
    .body(JSON.stringify(jsonBody), "application/json; charset=UTF-8")
    .put(url);
  const status = response.getStatusCode();
  const responseTxt = response.getResponseAsString();
  if (status !== 200) {
    engine.log(responseTxt);
    const error = `Failed to create Shared Link \n status: ${status}`;
    throw error;
  }
  
  const jsonRes = JSON.parse(responseTxt);

  return jsonRes.shared_link.url;
  }

Download

Capture

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.
%d bloggers like this: