0
votes

I am building an app that uses box, and I want to add a collaboration on my folder for a given user.

Looking at box doc I am doing the following :

const assignUserToFolder = function(appUserId) {

    //Get App auth client
    const boxAdminClient = 
        BoxSdk.getAppAuthClient('enterprise', process.env.BOX_ENTERPRISE_ID);
    console.log(`assign user ${appUserId} to folder ${process.env.BOX_FOLDER_ID}`);
    boxAdminClient.collaborations.createWithUserID(
      appUserId, 
      process.env.BOX_FOLDER_ID, 
      boxAdminClient.collaborationRoles.EDITOR, (
        function (error, boxResponse) {
          if (error) {
            console.log(`error ${error}`);
          }
    }));

};

I have verified that the box client is correct and appUserId and the folderId have correct values.

I have also tested directly using the API directly and I was able to update and set the correct role for my folder.

but even when I put the equivalent to my node code

const assignUserToFolderAPI = function(appUserId) {
    var requestParams = {
      body: {
        item: {
          id: process.env.BOX_FOLDER_ID,
          type: "folder"
        },
        accessible_by: {
          id: appUserId,
          type: "user"
        },
        role: "editor"
      }
    };

    //Get App auth client
    const boxAdminClient = BoxSdk.getAppAuthClient('enterprise', process.env.BOX_ENTERPRISE_ID);
    return new Promise(function (resolve, reject) {
        //Create the collaboration in Box
        console.log(`create collaboration for user ${appUserId}`);
        boxAdminClient.post('/collaborations', requestParams, boxAdminClient.defaultResponseHandler(function(error, boxResponse) {
                if (error) {
                reject(error);
                console.log(requestParams);
                  }

                  resolve(boxResponse);
          }));
    });
};

I get the following error messsage

"errorMessage": "Unexpected API Response [404 Not Found] (not_found: \"Not Found\")",

When I review the requestParams, both item.id and accessible_by.id are correct, and it works just fine from CLI.

Does anyone know why this would not work ? could it be an issue with the service account I am using ?

In the box app:

  • Application Access is Enterprise
  • I've selected all Application Scopes
3

3 Answers

1
votes

Your code seems to run fine when I ran it passing an appUserId and a valid folderId. What is the error you are getting?

1
votes

Ok, it ended up having nothing to do with the version. This should work...

adminAPIClient.enterprise.getUsers({filter_term: FOLDER_OWNER_EMAIL}, function (err, users) {
 var owner = users.entries[0];
 var userAPIClient = sdk.getAppAuthClient('user', owner.id);  // folder owner id

userAPIClient.collaborations.createWithUserID(
APP_USER_ID,   
FOLDER_ID, 
userAPIClient.collaborationRoles.EDITOR, 
function(err, data) {
  console.log(err);
   });
 });
0
votes

try running this to see the collaborations on that folder:

boxAdminClient.folders.getCollaborations('xxxxxx', null, function(err, data) {
      console.log(data);
      data.entries.forEach(function(element) {
          console.log(element)
      }, this);
  })