Python List all files and folder in Box
Hi everyone I try to list all files and folder on my Box.com to find folder&file name I need to download (cuz tooo many files in a folder) I tried example in this repo but it didnt help the errors like this:
GET https://api.box.com/2.0/collections/1104730352/items {'headers': {'Authorization': '---rer ',
'User-Agent': 'box-python-sdk-3.2.0',
'X-Box-UA': 'agent=box-python-sdk/3.2.0; env=python/3.8.10'},
'params': {'offset': 0}}
"GET https://api.box.com/2.0/collections/1104730352/items?offset=0" 400 0
{'Date': 'Sun, 27 Mar 2022 16:31:58 GMT', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'strict-transport-security': 'max-age=31536000', 'www-authenticate': 'Bearer realm="Service", error="invalid_request", error_description="Malformed auth header"', 'box-request-id': '0553f4e364e940a351913e2050de483b3', 'x-envoy-upstream-service-time': '1'}
b''
Traceback (most recent call last):
File ".\box.py", line 12, in <module>
for item in items:
File "C:\ProgramData\Miniconda3\lib\site-packages\boxsdk\pagination\box_object_collection.py", line 81, in next
return next(self._all_items)
File "C:\ProgramData\Miniconda3\lib\site-packages\boxsdk\pagination\box_object_collection.py", line 87, in _items_generator
response_object = self._load_next_page()
File "C:\ProgramData\Miniconda3\lib\site-packages\boxsdk\pagination\box_object_collection.py", line 124, in _load_next_page
box_response = self._session.get(self._url, params=params)
File "C:\ProgramData\Miniconda3\lib\site-packages\boxsdk\session\session.py", line 90, in get
return self.request('GET', url, **kwargs)
File "C:\ProgramData\Miniconda3\lib\site-packages\boxsdk\session\session.py", line 134, in request
response = self._prepare_and_send_request(method, url, **kwargs)
File "C:\ProgramData\Miniconda3\lib\site-packages\boxsdk\session\session.py", line 337, in _prepare_and_send_request
self._raise_on_unsuccessful_request(network_response, request)
File "C:\ProgramData\Miniconda3\lib\site-packages\boxsdk\session\session.py", line 264, in _raise_on_unsuccessful_request
raise BoxAPIException(
boxsdk.exception.BoxAPIException: Message: None
Status: 401
Code: None
Request ID: None
Headers: {'Date': 'Sun, 27 Mar 2022 16:32:31 GMT', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'x-envoy-upstream-service-time': '10', 'strict-transport-security': 'max-age=31536000', 'www-authenticate': 'Bearer realm="Service", error="invalid_token", error_description="The access token provided is invalid."', 'box-request-id': '0b4f93286e170cdf0a1ef2994fc8d8d0a'}
URL: https://api.box.com/2.0/collections/1104730352/items
Method: GET
Context Info: None
The code I am using like these:
from boxsdk import DevelopmentClient
client = DevelopmentClient()
items = client.collection(collection_id='1104730352').get_items()
for item in items:
print(f'{item.type.capitalize()} "{item.name}" is in the collection')
and
from boxsdk import DevelopmentClient
client = DevelopmentClient()
client.user('me').get()
from boxsdk.exception import BoxAPIException
try:
client.file('0').get()
except BoxAPIException as e:
pass
and
from boxsdk import Client, OAuth2
# Create an SDK client using a developer token
client = Client(OAuth2(None, None, access_token='6jxGtALyPTjcodkx738GTl7Q0cdLQKrP'))
SHARED_LINK_URL = 'https://app.box.com/s/3ckbonl3vsxl5q24u8adl23b9g01hr08'
shared_item = client.get_shared_item(SHARED_LINK_URL)
# print(shared_item.name)
# cl=client.file(shared_item.id).get()
# print(cl)
print(client.with_shared_link(SHARED_LINK_URL, None).file(shared_item.id).get())
I want to list: ID, Name, Link to file/folder
can any one help me to solve? thank you very much
Hello @anh-ai, In your first example you are getting 401 (Unauthorised) response which suggests you haven't provided any authorisation token. The first two examples should be run in REPL as they are interactive and will prompt for your input. If you want to create a script to run I suggest following the authentication guide: https://github.com/box/box-python-sdk/blob/main/docs/usage/authentication.md#developer-token This snippet will help you start:
from boxsdk import Client, OAuth2
auth = OAuth2(
client_id='YOUR_CLIENT_ID',
client_secret='YOUR_CLIENT_SECRET',
access_token='DEVELOPER_TOKEN_GOES_HERE',
)
client = Client(auth)
me = client.user().get()
print(f'My user ID is {me.id}')
You will find client ID and secret with developer token in your application settings: https://app.box.com/developers/console. Once you have this working please checkout:
- https://github.com/box/box-python-sdk/blob/main/docs/usage/folders.md
- https://github.com/box/box-python-sdk/blob/main/docs/usage/files.md.
Those will provide you with guidance what to do next.
This issue has been automatically marked as stale because it has not been updated in the last 30 days. It will be closed if no further activity occurs within the next 7 days. Feel free to reach out or mention Box SDK team member for further help and resources if they are needed.
This issue has been automatically closed due to maximum period of being stale. Thank you for your contribution to Box Python SDK and feel free to open another PR/issue at any time.