Skip to main content
Question

Python upload script

  • May 22, 2025
  • 3 replies
  • 65 views

Forum|alt.badge.img

This does not work with box sdk on python

from boxsdk import OAuth2, client

auth = OAuth2(
    client_id='xxxx',
    client_secret='xxx'
)

auth_url, csrf_token = auth.get_authorization_url('https://app.box.com')

folder_id = '123456'
new_file = client.folder(folder_id).upload.upload('test.txt')
print(f'File "{new_file.name}" uploaded to Box with file ID {new_file.id}')
 
it says:
Traceback (most recent call last):
  File "p_box_test.py", line 12, in <module>
    new_file = client.folder(folder_id).upload.upload('test.txt')
AttributeError: module 'boxsdk.client.client' has no attribute 'folder'

when i change the "client" to "Client" then i get this:
Traceback (most recent call last):
  File "p_box_test.py", line 12, in <module>
    new_file = Client.folder(folder_id).upload.upload('test.txt')
TypeError: folder() missing 1 required positional argument: 'folder_id'

Before you start showing off your intelectual superiority by telling me to read the development SDK again, i am working just fine with Trello, Slack and Ansible API using Python, and i do not want to stampede to start a more colloquial terms. Thanks in advance for helping me out.

3 replies

Forum|alt.badge.img

Curl does not work as well, what credentials should i use here?

https://developer.box.com/guides/uploads/direct/file/

curl -i -X POST "https://upload.box.com/api/2.0/files/content" -H "Authorization: Bearer ???????????????? " -H "Content-Type: multipart/form-data" -F attributes='{"name":"test.txt", "parent":{"id":"159248921121"}}' -F file=@test.txt

With Ansible or Trello it's easy, i just put my Oauth2 token, but here? I tried my apps developer token.

HTTP/1.1 403 Forbidden
Date: Fri, 25 Mar 2022 12:25:53 GMT
Content-Length: 0
Connection: keep-alive
WWW-Authenticate: Bearer realm="Service", error="insufficient_scope", error_description="The request requires higher privileges than provided by the access token."
X-Envoy-Upstream-Service-Time: 189
Strict-Transport-Security: max-age=31536000

 


Forum|alt.badge.img

Your tool is garbage. I just need to upload reports from automation. Our company is going to switch to MS Onedrive, i am going to investigate the possibilities over there. Bye.


Forum|alt.badge.img

Jozef,

In your curl example you can use a bearer token which is the access token. You can grab that from Box developer console. See https://developer.box.com/guides/authentication/tokens/developer-tokens or from https://developer.box.com/reference/post-oauth2-token/

In Python SDK, I see 2 upload (new_file = client.folder(folder_id).upload.upload('test.txt')
) not sure if that is a typo. If you can provide the complete file then I can run it in my environment and try to see the issue. You can upload it to Box and provide a link or I can raise a File request for you.

Thanks

Rohit