How to upload a file a folder shared by other

SOLVED
Go to solution
Highlighted
New Contributor

How to upload a file a folder shared by other

Hi everyone!

I follow Doc API and was able to upload a file to my Enterprise web app. I can view file already.

 

from boxsdk import JWTAuth
from boxsdk import Client
import json
auth = JWTAuth.from_settings_file(r'<path to config>')

access_token = auth.authenticate_instance()

from boxsdk import Client

client = Client(auth)

file_path = r'<path to file>'
file_name = 'toan'
folder_id =***phone number removed for privacy***' # I change to shared folder id to upload file
user_id = '116510981726'

user = client.user(user_id=user_id).get()
print(user.login)

user_to_impersonate = client.user(user_id=user_id)
user_client = client.as_user(user_to_impersonate)

 

Now I want to upload file to a folder which is shared by my colleague.

How can I do that? File name 'toan' is the file I uploadedcontent.png

 

I got a message like this:

File "C:\Users\nguye\AppData\Local\Programs\Python\Python38\lib\site-packages\boxsdk\session\session.py", line 406, in _prepare_and_send_request
self._raise_on_unsuccessful_request(network_response, request)
File "C:\Users\nguye\AppData\Local\Programs\Python\Python38\lib\site-packages\boxsdk\session\session.py", line 320, in _raise_on_unsuccessful_request
raise BoxAPIException(
boxsdk.exception.BoxAPIException: Message: Not Found
Status: 404
Code: not_found

 

Appreciate your help

Thank you very much!

2 REPLIES 2
Highlighted
Box Employee

Re: How to upload a file a folder shared by other

Hey @JonathanEdward , I think you might be running into an odd part of the as-user design, where you can't actually take actions on content external to your enterprise when you impersonate a managed user with "as-user". This is the same reason why you only see "External folder" in your admin console screenshot. 

 

In terms of Box's product offerings, we have two ways for apps to impersonate users - one is as-user, which is designed to reflect one user (one access token) impersonating another user. The second way is generating a token for the user directly, which a flow specific to JWT auth. This is the flow you want, and you can do this in python with this excerpt:

https://github.com/box/box-python-sdk/blob/master/docs/usage/authentication.md#server-auth-with-jwt

 

You'll know you've done it right when you're able to return information about the external folders via the API.

 

Hope that helps,

Jason

Highlighted
New Contributor

Re: How to upload a file a folder shared by other

Hi Jason,

I actually passed through document several times but cant get the idea. Now seem OK will few lines of code.

 

auth = JWTAuth.from_settings_file(r'path-to-config-json')
client = Client(auth)
auth.authenticate_user('USER-ID')
user_client = Client(auth)
file_path = r'PATH-TO-UPLOAD-FILE'
box_file = user_client.folder('ROOT-FOLDER-ID').upload(file_path)


Thank you very much.