File not getting downloaded from box when trying with Python boxsdk
回答済み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.
-
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
-
There are a few ways:
- You can use the search endpoint to search for content, which will return the IDs.
- You can get all items in a specific folder, which will also return the IDs.
- You can also search via metadata, which will return the file information.
- Jon
-
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().idbox_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()*************************************************************************************
-
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.
-
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)
-
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/
サインインしてコメントを残してください。
コメント
11件のコメント