403 error in uploading file to Box via Python boxsdk

22 views Asked by At

I am trying to upload a file to Box via python boxsdk.

I am generating the access token using the below function:

def get_access_token(client_id, client_secret):
    # Define Box application credentials
    client_id = client_id
    client_secret = client_secret

    # Make a POST request to obtain the access token
    token_url = 'https://app.box.com/oauth2/token'
    data = {
        'grant_type': 'client_credentials',
        'client_id': client_id,
        'client_secret': client_secret
    }
    response = requests.post(token_url, data=data)
    print(response.json())
    # Parse the response
    if response.status_code == 200:
        access_token = response.json()['access_token']
        print(f'Access token: {access_token}')
        return access_token
    else:
        print('Failed to obtain access token')

Then I am trying to upload a file from my local folder to my box app using the below function:

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)

    new_file = client.folder(destination_folder_id).upload(file_to_upload)
    print(f'File "{new_file.name}" uploaded to Box with file ID {new_file.id}')

However, I am getting a 403 error message:

boxsdk.exception.BoxAPIException: Message: None
Status: 403
Code: None
Request ID: None
Headers: {'Content-Type': 'text/html', 'Server': 'Zscaler/6.2', 'Cache-Control': 'no-cache', 'Access-Control-Allow-Origin': '*', 'Content-length': '14419'}
URL: https://upload.box.com/api/2.0/files/content
Method: POST
Context Info: None

Any help will be appreciated. Thanks.

0

There are 0 answers