I am trying to upload a file to Box using Python. I have followed these steps:
- Create a custom app with JWT
- Set up the following settings:
- In "Configuration" tab, select "App Access Level" = "App Access Only"
- Under "Application Scopes", I checked the box for "write all files"
- Under "Advanced Features", I checked the box for "Make API calls using the as-user header"
- Click "Generate a Public/Private Key pair" and saved the file as config.json
- Authorize the custom application using the Client ID at Admin Console.
- Run this code: https://github.com/asen123/box-python-sdk-large-file-upload/blob/master/upoad.py
from boxsdk import JWTAuth
from boxsdk import Client
# read json configuration file
auth = JWTAuth.from_settings_file('config.json')
access_token = auth.authenticate_instance()
# initialize sdk client
client = Client(auth)
service_account = client.user().get()
print('Service Account user ID is {0}'.format(service_account.id))
#file name and path
file_name = 'FILE_NAME'
stream = open('PATH_TO_FILE', 'rb')
#box parameters
folder_id = '0'
user_id = '0'
user = client.user(user_id)
#make the call
box_file = client.as_user(user).folder(folder_id).upload_stream(stream, file_name)
print('File "{0}" uploaded to Box with file ID {1}'.format(box_file.name, box_file.id))
but in the second to last line, it throws this error:
BoxAPIException: Message: Access denied - insufficient permission
Status: 403
Code: access_denied_insufficient_permissions
Request ID: vbqcplgq1cbpg5cj
Headers: {'Date': 'Thu, 29 Apr 2021 21:53:18 GMT', 'Content-Type': 'application/json; charset=UTF-8', 'Content-Length': '217', 'Connection': 'keep-alive', 'X-Envoy-Upstream-Service-Time': '100', 'Strict-Transport-Security': 'max-age=31536000', 'Cache-Control': 'no-cache, no-store'}
URL: https://upload.box.com/api/2.0/files/content
Method: POST
Context Info: None
I also tried upload rather than upload_stream and got the same result. Does anyone know why I may still have insufficient permission?
I think your issue is with:
The
user_idshould be the ID of the App User account that has access to the folder in Box.https://developer.box.com/reference/post-users/
To create the account I usually use postman.
The Response will return some information about the user you just created, one part being the ID.
Add this new user to the folder you want to upload to and use the ID as the
as_userIDIn your code example. Update the user_id to be the ID returned.