Level up your Box knowledge with brand new learning paths on Box University. Visit training.box.com to get started

File not getting downloaded from box when trying with Python boxsdk

Answered
New post

Comments

11 comments

  • jcleblanc

    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

    0
    Comment actions Permalink
  • Sathish T R

    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?

    0
    Comment actions Permalink
  • jcleblanc

    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

    0
    Comment actions Permalink
  • Sathish T R

    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?

    0
    Comment actions Permalink
  • jcleblanc

    There are a few ways:

    - Jon

    0
    Comment actions Permalink
  • Sathish T R

    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()

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

    0
    Comment actions Permalink
  • jcleblanc

    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

    0
    Comment actions Permalink
  • Sathish T R

    I created the folder from my box account.

    0
    Comment actions Permalink
  • jcleblanc

    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.

    0
    Comment actions Permalink
  • Sathish T R

    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)
    0
    Comment actions Permalink
  • jcleblanc

    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/

    0
    Comment actions Permalink

Please sign in to leave a comment.