Box: Create Shared Link to Folder
This item 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.
Basic Configs
- Step Name
- Note
Configs for this Auto Step
- OAuth2
- C1: OAuth2 Setting *
- FolderId
- C2-deprecated: Folder ID to share#{EL}
- FolderId_V2
- C2: Folder ID to share *
- unsharedAt
- C3: Data item with Expiration of the Link
- DownloadPass
- C4: Data item with Password of the Link
- DownloadUrlItem
- C5: Data item that will save web the Shared Link *
Notes
- If C2-deprecated is set, you must set C2: Folder ID to share
- 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
- When the target folder contains watermarked files, if you set a password, sharer will not be able to download or preview those files (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-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 auto step
function main(){
const folderId = retrieveFolderId();
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.getObject("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);
}
}
/**
* config からフォルダ ID を読み出す。空文字列の場合はエラー
* @return {String} folderId フォルダ ID
*/
function retrieveFolderId() {
const folderIdV1 = configs.get("FolderId");
if (folderIdV1 !== null && folderIdV1 !== "") {
return folderIdV1;
}
let folderId = configs.get("FolderId_V2"); // 固定値の場合
const folderIdDef = configs.getObject("FolderId_V2");
if (folderIdDef !== null) { // 文字型データ項目が選択されている場合
folderId = engine.findData(folderIdDef);
}
if (folderId === null || folderId === "") {
throw "Folder ID is blank";
}
return folderId;
}
/**
* 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();
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();
if (status !== 200) {
engine.log(responseTxt);
throw "Failed to create Shared Link";
}
const jsonRes = JSON.parse(responseTxt);
return jsonRes.shared_link.url;
}
Pingback: Box File Upload (Password / Expiration) – Questetra Support
Pingback: Box: Delete Shared Link of Folder – Questetra Support
Pingback: Utilising Box From Your Workflow – Questetra Support