Box: コラボレーション追加

Box: コラボレーション追加

Box: Add Collaboration

この工程は、Box の指定ファイルもしくはフォルダに、指定ユーザとのコラボレーションを追加します。ユーザはメールアドレスで指定します。

Auto Step icon
Basic Configs
工程名
メモ
Configs for this Auto Step
conf_OAuth2
C1: OAuth2 設定 *
conf_EmailAddress
C2: 招待する Box ユーザのメールアドレス *
conf_Role
C3: 設定するロール *
conf_ItemType
C4: 項目の種類(ファイルまたはフォルダ) *
conf_ItemId
C5: コラボレーションを追加するファイル/フォルダの ID *

Notes

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

Capture

See also

Script (click to open)
  • 次のスクリプトが記述されている XML ファイルをダウンロードできます
    • box-collaboration-add.xml (C) Questetra, Inc. (MIT License)
    • Professional のワークフロー基盤では、ファイル内容を改変しオリジナルのアドオン自動工程として活用できます

function main(){

  const oauth2 = configs.getObject("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 {AuthSettingWrapper} 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`); 
  }
}
上部へスクロール

Questetra Supportをもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む