I am trying to automate a process for work in which a file gets uploaded to Box on a weekly cadence. So far, I can connect to the Box API but when I try to add myself as a user so that I can upload the file to a folder, I receive this error: {'error': 'invalid_request',
'error_description': 'Cannot obtain token based on the enterprise '
'configuration for your app'}
Any help would be greatly appreciated. In the configuration settings I have already checked the box to "Generate user access tokens" so I'm at a loss as to what the issue is now. Here is my code:
from boxsdk import JWTAuth, Client
config_file_name = r'C:\Users\7J4750897\PycharmProjects\box_upload_test_jwt\908919732_hgonu854_config.json'
#Parse the values from the Configuration file for the JWTAuth object
auth = JWTAuth.from_settings_file(config_file_name)
# Tell the SDK to get a token so that we can print it.
access_token = auth.authenticate_instance()
# This token is live. You could use cURL or Postman or the Requests library with this token to make API calls.
# See the API docs at developer.box.com for more info
print(f'Current Service Account Access Token: {access_token}')
#Initialize a Client object to wrap the Auth object
client = Client(auth)
#Get the current user's information (GET /users/me)
user = client.user().get()
print(f'Service Account user ID is {user.id}')
print(user['name'])
print(user['login'])
print('Connected to Box API with user {}'.format(client.user().get().login))
#Add myself as a user
mfg_user = client.user('19863021705')
mfg_auth = JWTAuth.from_settings_file(config_file_name)
mfg_auth.authenticate_app_user(mfg_user)
mfg_client = Client(mfg_auth)
print(f'Service Account user ID is {mfg_client.id}')
print(mfg_client['name'])
print(mfg_client['login'])
#Upload new file
folder_id = '166942895701'
new_file = client.folder(folder_id).upload(r'C:\Users\7J4750897\PycharmProjects\box_upload_test\personal_monthly_budget.xlsx')
print(f'File "{new_file.name}" uploaded to Box with file ID {new_file.id}')