Box: Create Shared Link to File
This item creates a URL to access the specified file on Box. Created with no expiration and/or no password requests if you leave them blank.
Configs: Common
  • Step Name
  • Note
Configs
  • C1: OAuth2 Setting *
  • C2: File ID to share *
  • C3: Expiration of the Link
  • C4: Password of the Link
  • C5: Data item that will save the Shared Link *

Notes

  • The folder ID is contained in the URL: https://{sub-domain}.app.box.com/file/(File ID)
  • Box refresh tokens have an expiration date
  • You cannot recreate a shared link in a file that already has a shared link
  • If you set a password on a watermarked file, sharer will not be able to download or preview it (as of 2022-10-28)

Capture

See also

Script (click to open)
  • An XML file that contains the code below is available to download
    • box-file-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

main();
function main() {
    // get OAuth2 Setting
    const oauth2 = configs.get("conf_OAuth2");
    const fileId = decideFileId();
    const unsharedDateDef = configs.getObject("conf_UnsharedAt");
    const passwordDef = configs.getObject("conf_Password");

    let unsharedDate = null;
    if (unsharedDateDef !== null) {
        unsharedDate = engine.findData(unsharedDateDef);
    }

    let password = "";
    if (passwordDef !== null) {
        password = engine.findData(passwordDef);
    }

    checkExistingSharedLink(oauth2, fileId);
    const sharedlinkUrl = createSharedLink(oauth2, fileId, unsharedDate, password);

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


/**
  * ファイルの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 `Shared link(${jsonRes.shared_link.url}) was already created.`;
    }
}


/**
  * Create Shared Link to File on Box  共有リンク作成
  * @param {String} oauth OAuth2 設定
  * @param {String} fileId ファイルの ID
  * @param {AddableTimestamp} unsharedDate 有効期限
  * @param {String} password パスワード
  * @return {String}  ファイルの共有リンクURL
  */
function createSharedLink(oauth2, fileId, unsharedDate, password) {
    let timezone = engine.getTimeZoneId();
    if (timezone === "GMT") {
        timezone += "+00:00";
    }

    const 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/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) {
        engine.log(responseTxt);
        throw `Failed to create Shared Link. status:${status}`;
    }
    const jsonRes = JSON.parse(responseTxt);
    return jsonRes.shared_link.url;
}

2 thoughts on “Box: Create Shared Link to File”

  1. Pingback: Box: Delete Shared Link of File – Questetra Support

  2. Pingback: Box: Add Collaboration – Questetra Support

Comments are closed.

%d bloggers like this: