Unable to upload files to Box APP

Hi,

The app that I have created is ‘Authenticated’ using: OAuth 2.0 (User or Client Authentication)

I have a free developer account and I am trying to upload a file using boxsdk. However, I am getting the below error:

boxsdk.exception.BoxAPIException: Message: Not Found
Status: 404
Code: not_found
Request ID: 5g33vbho4p64txku
Headers: {'Server': 'nginx', 'Date': 'Tue, 26 Mar 2024 13:05:47 GMT', 'Content-Type': 'application/json', 'Content-Length': '324', 'cache-control': 'no-cache, no-store', 'x-envoy-upstream-service-time': '505', 'strict-transport-security': 'max-age=31536000', 'X-Box-Original-Ingress-ADC-Host': 'prod-b-traffic-manager-0q2f', 'Via': '1.1 google', 'Alt-Svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000'}
URL: https://upload.box.com/api/2.0/files/content
Method: POST
Context Info: {'errors': [{'reason': 'invalid_parameter', 'name': 'parent', 'message': "Invalid value 'd_254434451261'. 'parent' with value 'd_254434451261' not found"}]}

I have ensured the my folder_id is correct.

Below is my code. The ‘Access Token’ is being generated. However, the error is coming when uploading the file

def upload_file_main(client_id, client_secret, access_token, file_to_upload, destination_folder_id):
    # Set up authentication
    auth = OAuth2(
        client_id = client_id,
        client_secret=client_secret,
        access_token=access_token,
    )

    # Initialize Box client
    client = Client(auth)

    client.folder(destination_folder_id).upload(file_to_upload)

My app is not authorized and I am unable to view the ‘Authorization’ tab in my Developer Console:

Hi @user214 , welcome to the forum.

From the error message, it looks like the user that authenticated, does not have access to that specific folder.

Let us know if this helps

Best regards

Hi Rui,

Thank you for your reply.

While generating the access_token, when I keep the grant_type as ‘authorization_code’, I have to pass in the Authorization Code which needs to be generated manually by the user from the browser using the auth_url. When I request the access_token this way, I can upload files to the app.

However, I need to automate the process to upload files to the app on a weekly basis. Will the client/user always need to regenerate the authorization_code in this case? I have also read that the refresh_token will also expire after 1 use:

Thanks,
Aadish

Hi Aadish,

If you want to use this in a script then your best bet is to use a Client Credentials Grant type of authentication as opposed to OAuth 2.0

We have a guide available for this, but feel free to post any questions you might have.

This type of application will have a default service account, represented by the enterprise id, but it can also impersonate any user if given the permissions to do so.

This to highlight that the service account must be explicitly granted access to the files or folders you want it to access.

Looking at your code, looks like you are using the classic python sdk (there is a new Next Gen Python SDK) if you are interested.

Assuming you would put the client id, client secret and enterprise id in a .env file like so:

# Common settings
CLIENT_ID = YOUR_CLIENT_ID
CLIENT_SECRET = YOU_CLIENT_SECRET

# CCG Settings
ENTERPRISE_ID = YOUR_ENTERPRISE_ID

And you pip installed something like python-dotenv, the code would look something like this:

import os
import dotenv
from boxsdk import CCGAuth, Client

class ConfigCCG:
    """application configurations"""

    def __init__(self) -> None:
        dotenv.load_dotenv(ENV_CCG)
        # Common configurations
        self.client_id = os.getenv("CLIENT_ID")
        self.client_secret = os.getenv("CLIENT_SECRET")

        # CCG configurations
        self.enterprise_id = os.getenv("ENTERPRISE_ID")


def main():
    conf = ConfigCCG()
    auth = CCGAuth(
        client_id=conf.client_id,
        client_secret=conf.client_secret,
        enterprise_id=conf.enterprise_id,
    )
    client = Client(auth)

    user = client.user().get() 
    print(f"User: {user.id}:{user.name}")

    client.folder(destination_folder_id).upload(file_to_upload)

if __name__ == "__main__":
    main()
    print("Done")

Let us know if this helps.

Hi Rui,

Thanks for your help :slight_smile: