2

I am trying to upload a file to Box using Python. I have followed these steps:

  1. Create a custom app with JWT
  2. 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"
  3. Click "Generate a Public/Private Key pair" and saved the file as config.json
  4. Authorize the custom application using the Client ID at Admin Console.
  5. 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?

pattidegner
  • 107
  • 1
  • 10

1 Answers1

0

I think your issue is with:


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

The user_id should 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.


curl --location --request POST 'https://api.box.com/2.0/users' \
--header 'Authorization: Bearer {Your App Dev Token}' \
--header 'Content-Type: application/json' \
--data-raw '{"name": "{Name for New User}", "is_platform_access_only": true}'

The Response will return some information about the user you just created, one part being the ID.

{
  "id": 11446498,
  ...
  "name": "{Name for New User}",
  ...
}

Add this new user to the folder you want to upload to and use the ID as the as_user ID

In your code example. Update the user_id to be the ID returned.

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

KJDII
  • 851
  • 4
  • 11
  • I have my Box user ID in there, I just didn't want to post my personal info online. – pattidegner Apr 29 '21 at 22:49
  • Yeah I figured.. Whenever I use the custom app with JWT - the `as_user` is the service account ID I create, not my ID or any other actual user's. - but I also have to grant that app user permission to the folder I want to interact with. – KJDII Apr 29 '21 at 22:55
  • Updated the Answer to include the Docs I usually use. – KJDII Apr 29 '21 at 23:08