Box: コラボレーション追加 (Box: Add Collaboration)
この工程は、Box の指定ファイルもしくはフォルダに、指定ユーザとのコラボレーションを追加します。ユーザはメールアドレスで指定します。
Configs:共通設定
  • 工程名
  • メモ
Configs
  • C1: OAuth2 設定 *
  • C2: 招待する Box ユーザのメールアドレス *
  • C3: 設定するロール *
  • C4: 項目の種類(ファイルまたはフォルダ) *
  • C5: コラボレーションを追加するファイル/フォルダの ID *

Notes

  1. 既にコラボレーションされているユーザはエラーとなります。
  2. Box のリフレッシュトークンには、期限があります。期限を超えないよう、定期的に利用する必要があります。

Capture

See also

Script (click to open)
  • 下記のスクリプトを記述した XML ファイルをダウンロードできます
    • box-collaboration-add.xml (C) Questetra, Inc. (MIT License)
    • Professional をご利用であればファイルの内容を改変することでオリジナルのアドオンとして活用できます

main();
function main(){

  const oauth2 = configs.get("conf_OAuth2");
  const itemType = configs.get("conf_ItemType");
  const fileOrFolderId = decideFileOrFolderId(itemType);
  const emailAddress = decideEmailAddress();
  const role = configs.get("conf_Role");
  
  // コラボレーション追加
  addCollaboration(oauth2, emailAddress, role, itemType, fileOrFolderId);
}


/**
  * 指定するメールアドレスをconfigから読み出して出力する
  * @return {String}  emailAddress メールアドレス
  */
function decideEmailAddress(){
  let emailAddress = "";
  const emailAddressDef = configs.getObject("conf_EmailAddress");
  if(emailAddressDef === null){
    emailAddress = configs.get("conf_EmailAddress");
  }else{
    emailAddress = engine.findData(emailAddressDef);
 }

  if(emailAddress === "" || emailAddress === null) {
    throw "Email address is blank";
  }
  return emailAddress;
}


/**
  * コラボレーションするファイルもしくはフォルダのIDをconfigから読み出して出力する
  * @param {String}  itemType アクセス権限が付与される項目の種類 ファイル/フォルダ
  * @return {String} id ファイル もしくは フォルダの ID
  */
function decideFileOrFolderId(itemType){
  let id = "";
  const idDef = configs.getObject("conf_ItemId");
  if(idDef === null){
    id = configs.get("conf_ItemId");
  }else{
    id = engine.findData(idDef);
  }

  if(id === "" || id === null) {
    throw "File ID or Folder ID is blank";
  }
  if(itemType === "folder" && id === "0") {
    throw "Root folder cannot be collaborated";
  }

  return id;
}


/**
  * コラボレーションを追加する
  * @param {String} oauth2 Oauth2設定
  * @param {String} emailAddress コラボレーションするユーザのメールアドレス
  * @param {String} role ロール
  * @param {String} itemType アクセス権限が付与される項目の種類 ファイル/フォルダ
  * @param {String} fileOrFolderId コラボレーションしたいファイル/フォルダのID
  */
function addCollaboration(oauth2, emailAddress, role, itemType, fileOrFolderId) {
  const jsonReq = {};
  jsonReq["item"] = {"id": fileOrFolderId, "type": itemType};
  jsonReq["accessible_by"] = {"login": emailAddress };
  jsonReq["role"] = role;
  
  const url = 'https://api.box.com/2.0/collaborations';
  const response = httpClient.begin()
    .authSetting(oauth2)
    .queryParam("fields", "invite_email,role,accessible_by")
    .body(JSON.stringify(jsonReq), "application/json; charset=UTF-8")
    .post(url);
  const status = response.getStatusCode();
  const responseTxt = response.getResponseAsString();

  let jsonRes;
  try {
     jsonRes = JSON.parse(responseTxt);
  } catch(e) {
     engine.log(`${responseTxt}`);
     engine.log("failed to parse as json");
     throw `Failed to add collaboration. status: ${status}`;
  }

  if (status !== 201){
    engine.log(`${responseTxt}`);
    if(status === 400 && jsonRes["code"] === "user_already_collaborator"){
      throw `This request is not needed, ${emailAddress} already collaborator. status: ${status}`;
    }else{
      throw `Failed to add collaboration. status: ${status}`;
    }
  }

  if (jsonRes["invite_email"] === null ){
    engine.log(`${jsonRes.accessible_by.login} collborated as ${jsonRes.role}`);
  }else{
    engine.log(`${jsonRes.invite_email} collborated as ${jsonRes.role}, and Box sends an invitation email`); 
  }
}
%d人のブロガーが「いいね」をつけました。