Skip to main content


Got an interesting question from a developer:





I have been shared a Box folder via the following link:



https://asmodeeuk.app.box.com/v/HobbyOrder



I would like to programatically access these files in python or at least download them locally.



I have set up a developer box account and I am just using the developer token for now.



TOKEN = 'DEV_TOKEN'

auth = OAuth2(None, None, access_token=TOKEN)

box = Client(auth)



I have also added this to my favourites in Box.



favourites_id = 8166214818

items = box.collection(collection_id=favourites_id).get_items()

for item in items:

print(item.id)



I can use the above code to see that the folder is in my favourites collection



{'etag': '0',

'id': '20105505368',

'name': 'Asmodee Box',

'sequence_id': '0',

'type': 'web_link',

'url': 'https://asmodeeuk.app.box.com/v/HobbyOrder'}



However I cannot figure out how to use the above to open the folder and list / download the items within.



Can anyone help with this next step? 🙂

So HobbyOrder shared with you a web_link. In Box these types of object behave similar to the File and Folder object with some limitations, but for this purpose it works fine.



The first step is to get the shared link as an object from the URL provided:



auth = JWTAuth.from_settings_file('.jwt.config.json')

auth.authenticate_instance()

client = Client(auth)



web_link_url = "https://asmodeeuk.app.box.com/v/HobbyOrder"



user = client.user().get()

print(f"User: {user.id}:{user.name}")



shared_folder = client.get_shared_item(web_link_url,'' )

print(f"Shared Folder: {shared_folder.id}:{shared_folder.name}")

print("#" * 80)



I’m using JWT but it is the same, and we get:



User: 20344589936:UI-Elements-Sample

Shared Folder: 69272724281:GBP Order Forms

###################################################################



From here you can immediately list the files and folder with something like:



items = shared_folder.get_items()

for item in tems:

print(f"{item.type}\t{item.id}\t{item.name}")



Resulting in (abbreviated version):



file    719978593854    Pre-Order Form.xlsx

file 1069687190434 Sale List 18.11.22.xlsx

file 690926384396 Special Order List.xlsx

file 416472888263 Stock List.xlsx

file 790892691217 Upcoming Restock lines.xlsx



There is a way to download a zip file with all the content, but it does not work with web links.


So we need to download the files one by one, and since it does have folders inside, we need to do it recursively. Here is the full example:



import os

from boxsdk import JWTAuth, Client





def main():

auth = JWTAuth.from_settings_file('.jwt.config.json')

auth.authenticate_instance()

client = Client(auth)



web_link_url = "https://asmodeeuk.app.box.com/v/HobbyOrder"



user = client.user().get()

print(f"User: {user.id}:{user.name}")



shared_folder = client.get_shared_item(web_link_url,'' )

print(f"Shared Folder: {shared_folder.id}:{shared_folder.name}")

print("#" * 80)



print("Type\tID\t\tName")

os.chdir('downloads')

items = shared_folder.get_items()

download_items(items)

os.chdir('..')



def download_items(items):



for item in items:

if item.type == 'folder':

os.mkdir(item.name)

os.chdir(item.name)

download_items(item.get_items())

os.chdir('..')



if item.type == 'file':

print(f"{item.type}\t{item.id}\t{item.name}",end='')

with open(item.name,'wb') as download_file:

item.download_to(download_file)

print("\tdone")





if __name__ == "__main__":

main()

print("Done")



And the full output:



User: 20344589936:UI-Elements-Sample

Shared Folder: 69272724281:GBP Order Forms

################################################################################

Type ID Name

file 1105897541070 New Releases GBP 04.01.2023.xlsx done

file 1084864744961 New Releases GBP 07.12.22.xlsx done

file 1111312196308 New Releases GBP 11.01.2023.xlsx done

file 1090798714731 New Releases GBP 14.12.22.xlsx done

file 1067855890770 New Releases GBP 16.11.22.xlsx done

file 1116899009477 New Releases GBP 18.01.2023.xlsx done

file 1073707745044 New Releases GBP 23.11.22.xlsx done

file 1102088360194 New Releases GBP 28.12.22.xlsx done

file 1079316674927 New Releases GBP 30.11.22.xlsx done

file 1038085481617 CMON Pricing Update - from 7th November. GBP.xlsx done

file 1019510777741 Looney Labs Pricing Update GBP - from 01.10.22.xlsx done

file 1038092739093 Modiphius Pricing Update - from 7th November. GBP.xlsx done

file 945109278794 Pricing Update Steamforged Games and Gale Force 9 - From 1st May .xlsx done

file 944590782277 Pricing Update Wise Wizard Games - From 1st May .xlsx done

file 940408186248 Pricing Update WotC- From 19th April.xlsx done

file 1120889985645 Marvel Champions and Marvel Licensed Sale 23.01.23 GBP.xlsx done

file 719978593854 Pre-Order Form.xlsx done

file 1069687190434 Sale List 18.11.22.xlsx done

file 690926384396 Special Order List.xlsx done

file 416472888263 Stock List.xlsx done

file 779991903042 Upcoming Allocations Expected.xlsx done

file 1081028856094 Upcoming Releases Pricing Update 02.12.22.xlsx done

file 790892691217 Upcoming Restock lines.xlsx done

Done


Reply