Skip to main content
Question

File not getting downloaded from box when trying with Python boxsdk

  • May 22, 2025
  • 11 replies
  • 55 views

Forum|alt.badge.img

Here is my code:

**************************************************************

from boxsdk import JWTAuth, Client

sdk = JWTAuth.from_settings_file('****_config.json')

client = Client(sdk)

service_account = client.user().get()
print('Service Account user ID is {0}'.format(service_account.id))

user=client.user(user_id = service_account)

root_folder = client.root_folder().get()
items = root_folder.get_items()

for item in items:
print('{0} {1} is named "{2}"'.format(item.type.capitalize(), item.id, item.name))
with open(item.name, 'wb') as open_file:
client.file(item.id).download_to("C:/"+open_file)
print('downloaded')
open_file.close()

*********************************************************************

Program runs fine but nothing getting downloaded to my C drive. 

11 replies

Forum|alt.badge.img

Hi Sathish,

What's the response object from Box when you are attempting to download the file? Are you seeing an error, or a valid response?

- Jon


Forum|alt.badge.img

Hi,

there is no response object getting created in the above code. 

I found this code while browsing for creating response object: response = requests.post(authentication_url, params)

What should I give in place of "params" here?


Forum|alt.badge.img

Hi Sathish,

For downloading a file, this is the code sample that should work: https://github.com/box/box-python-sdk/blob/master/docs/usage/files.md#download-a-file

If you're not seeing errors / responses from the call, you'll want to wrap the calls in a try/catch block or do some additional error logging to be able to retrieve the information. Most of the time cases like these will return a 404 / not found because due to the access token being scoped for the service account rather than the user who owns the file.

- Jon


Forum|alt.badge.img

All the sample code uses same methodology of getting input of file id. Lets say I do not know the file id, I fetch a new file uploaded by another user. How do I download files with specific name/pattern from a folder without file id?


Forum|alt.badge.img

There are a few ways:

- Jon


Forum|alt.badge.img

Hi,

I have only "App access". I can't get "Enterprise" access as per company's restrictions on box account. I am using below code trying to download a file from folder where I am a co-owner, but I am getting error:

"BoxAPIException: Message: Access denied - insufficient permission
Status: 403
Code: access_denied_insufficient_permissions
Request ID: s2gnkgouz4owxk9
Headers: {'Date': 'Wed, 24 Mar 2021 06:23:02 GMT', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Strict-Transport-Security': 'max-age=31536000', 'Cache-Control': 'no-cache, no-store', 'Content-Encoding': 'gzip', 'BOX-REQUEST-ID': '0799f780601ba90a23150eae88652de19'}
URL: https://api.box.com/2.0/folders/XXXXX/items
Method: GET
Context Info: None"

***********************************************************************************************

from boxsdk import JWTAuth, Client
import requests
# Configure JWT auth object
sdk = JWTAuth.from_settings_file('C:/Work/box/IBM box/XXXXXXX.json')

client = Client(sdk)
service_account=client.user().get().id

box_folder_id='XXXXXXXX'
user = client.user(user_id=service_account)

listitems = client.as_user(user).folder(folder_id=box_folder_id).get_items(limit=100, offset=0)

for item in listitems:
output_file = open(item.name, 'wb')
item.download_to(output_file)
output_file.close()

*************************************************************************************


Forum|alt.badge.img

Hi Sathish,

In this case, who is the co-owner of the content, is it a user (such as your personal account) or is it the service account (the app)?

- Jon


Forum|alt.badge.img

I created the folder from my box account.


Forum|alt.badge.img

Ok then I think that's the issue. With you code you seem to be trying to access the folder with your service account, not your personal Box account. For all intents and purposes, the service account is another user account (much like an app user). That service account doesn't have access to the folder, only your user account does. What you can do instead is to scope the access token to your user account (such as like this), then use that access token to make the request.


Forum|alt.badge.img

Below is what I found in documentation: Where do I find 1. JWT_KEY_ID, 2 rsa private file .PEM, 3. rsa private key passphrase ?

user = client.user(user_id='12345')

auth = JWTAuth(
    client_id='[CLIENT_ID]',
    client_secret='[CLIENT_SECRET]',
    user=app_user,
    jwt_key_id='[JWT_KEY_ID]',
    rsa_private_key_file_sys_path='[CERT.PEM]',
    rsa_private_key_passphrase='[PASSPHRASE]'
)
auth.authenticate_user()
user_client = Client(auth)

Forum|alt.badge.img

Hi Sathish,

Please refer to the following link for a walkthrough of all of that information: https://developer.box.com/guides/applications/custom-apps/jwt-setup/#public-and-private-key-pair

You create or have Box generate the keypair, then use that info with the key ID that is provided when you add it to your application. You can also use this guide to walk through the process of setting up the application: https://developer.box.com/guides/authentication/jwt/with-sdk/