Box: Create Shared Link to Folder
Create a URL to download the specified folder on Box
2019-08-13 (C) Questetra, Inc. (MIT License)
Configs
  • C1: OAuth2 Setting Name *
  • C2: Folder ID to share * #{EL}
  • C3: Date 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
  let token;
  token = httpClient.getOAuth2Token(configs.get("OAuth2"));
  
  let jsonBody = {};

  jsonBody["shared_link"] = {"access": "open"};
  if(unsharedDate !== null){
    jsonBody["shared_link"]["unshared_at"] = unsharedDate.toString();
  }
  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 >= 300) {
    const error = "Failed to create \n status:" + status + "\n" + responseTxt
    throw error;
  }
  engine.log("status:" + status);
  engine.log(responseTxt);
  
  
  const jsonRes = JSON.parse(responseTxt);

  const UrlData = configs.get("DownloadUrlItem");
  if (UrlData !== null || UrlData !== "") {
      engine.setDataByNumber(UrlData,jsonRes["shared_link"]["url"] );
  }
}

Download

Capture

Notes

  1. Folder ID is contained in the URL. https://{sub-domain}.app.box.com/folder/(Folder ID)
  2. The refresh token for Box has the expiration. Use regularly to ensure that it does not exceed the expiration. (60 days, as of July 2019. https://box-content.readme.io/docs/oauth-20)