Skip to main content
Question

Python SDK, as_user usage ...

  • May 21, 2025
  • 2 replies
  • 30 views

Forum|alt.badge.img

Hi,

 

I'm finally at a stage where I manage to create a directory structure in box to mimic what

we had in objective, and I can upload files into box.

 

But even though I authenticate as an admin user from my script and don't get an error message

for using .as_user( user ) the ownership of uploaded files is systems_internal (shared admin account).

 

def upload_file(client, target_folder, localFileWithPath, fileName, originalOwner, lastUpdatedBy, dateLastUpdate, boxUser='***number removed for privacy***'):
    user = client.user(user_id=boxUser )
    root_folder = client.folder(folder_id=target_folder)
    a_file = root_folder.upload( localFileWithPath,
            file_name= fileName ).as_user( user  ).metadata().create({'objective owner':
                originalOwner, 'last updated by' : lastUpdatedBy, 'last updated' : dateLastUpdate})
    print( a_file )

 

What am I doing wrong?

 

 

Cheers,

Andrej

2 replies

Forum|alt.badge.img

Hi there,

I suppose, you already have As-User enabled in your App. if not you have to have box do it for you via ticket.

 

Now,

  this line here:

root_folder = client.folder(folder_id=target_folder)

 i think it should it be  root_folder = client.as_user(user).folder(folder_id= target_folder)  as well. also for the upload.

I think for all statements you should include as-user if it is doing on behalf of other users. 

i hope this made sense. 

 

thanks, 

Bibek


Forum|alt.badge.img

Hi Bibek,

 

Thank you for the response.

Yes, box have kindly enabled as_user for my app.

I may give your suggestion a shot.

 

Cheers,

Andrej