Skip to main content
Question

Uploading Files and Folders to Box using API

  • May 22, 2025
  • 3 replies
  • 53 views

Forum|alt.badge.img

Hey, I want to upload and download files from my box using a script. For this, I used the box API.

For authorisation, if I use Oauth, I have to manually interact with the browser, which I don't want because I want everything to be automatic. 

So, I used authentication with the JWT approach following this link and then following it up further:

https://developer.box.com/docs/authentication-with-jwt

Using this, I have to create app users for the enterprise and then make requests through them. When I make an upload request using this app user, I don't know to where it is getting uploaded, but it is not getting uploaded to the place where my other folders are located( which I had uploaded manually in browser). Can someone help me upload the file to the same place where folders uploaded manually are located?

 

This is my code so far:

 

from boxsdk import JWTAuth

auth = JWTAuth(
client_id='za53hh8onu9xq1m8if5n7zyeydjcbyqs',
client_secret='iLnPkOl4E9lZI0WPBSZ******',
enterprise_id='42706131',
jwt_key_id='fbsgiwwh',
rsa_private_key_file_sys_path='/home/krishna/Music/Projects/auto-store/private.pem',
rsa_private_key_passphrase=str.encode('013a9d2245189e933270a0bb9e551d81')
)

access_token = auth.authenticate_instance()

from boxsdk import Client

client = Client(auth)

service_account = client.user().get()
print ('Service Account name: ' + service_account.name)
print ('Service Account login: ' + service_account.login)

ned_stark_user = client.create_user('Ned Stark')

ned_auth = JWTAuth(
client_id='za53hh8onu9xq1m8if5n7zyeydjcbyqs',
client_secret='iLnPkOl4E9lZI0WPBSZVx****',
enterprise_id='42706131',
jwt_key_id='fbsgiwwh',
rsa_private_key_file_sys_path='/home/krishna/Music/Projects/auto-store/private.pem',
rsa_private_key_passphrase=str.encode('013a9d2245189e933270a0bb9e551d81')
)
ned_auth.authenticate_app_user(ned_stark_user)
ned_client = Client(ned_auth)

# root_folder = ned_client.folder(folder_id='0').get()
# print 'folder owner: ' + root_folder.owned_by['login']
# print 'folder name: ' + root_folder['name']
items = client.folder(folder_id='0').get_items(limit=100, offset=0)
print items

 

3 replies

Forum|alt.badge.img

I found out. It was in fact in the FAQ's of box . 

With Oauth, once we get access and refresh tokens, if we store it in our system, we can request new ones on expiry without manually authorising again. 

Refer the FAQ's for more details.

https://community.box.com/t5/Box-Developer-Forum/FAQ-Box-Platform-and-APIs/m-p/14768#U14768

 

Also, this has been really useful in automatically requesting tokens on expiry.

https://github.com/wesleyfr/boxpython


Forum|alt.badge.img

Hello Krishna_nagaraj,

 

Could you pls share the script you are using to upload file to box --- which authenticates with OAuth2 ?

I hope you have written in python?

 

Thanks


Forum|alt.badge.img

Hi,

 

Could you pls share the script.?

Thanks