Accessing as User through JWT but not being able to access not-owned folders
AnsweredHi Everyone,
I have a fairly easy task in my hands but i am having some trouble understanding the documentation as I am unable to do it.
I have a process that needs to access a Box folder that I am a co-owner of and have been granted collaborative access and download a couple of files, this process is supposed to happen at early hours in the morning so it would be nice if no user interaction is needed. I have checked the forums and it seems that an App that logs through jwt is the recommended answer in similar cases. So I set up an application I create the Public and Private keys and I authorize it to act in behalf of the user (i.e. me).
If I use the developer key everything works fine, I can download the files correctly but if I try to access with JWT it only lists the folders and files that I have create and thus I cannot access the proper folder.
I am using the python SDK, but if I need to use the http API for it to work it is not a big deal.
This is the code I have right now:
from boxsdk import JWTAuth, Client def main(): user_id = "XXXXXXXXXX" sdk = JWTAuth.from_settings_file('key_config.json') client = Client(sdk) user = client.user(user_id=user_id).get() client = client.as_user(user) items = client.folder(folder_id='0').get_items() for item in items: print('{0} {1} is named "{2}"'.format(item.type.capitalize(), item.id, item.name))
main()
This list the user folders and files but only the ones owned by him.
If instead of accessing the root folder I try to access the shared folder I get a 404 "folder with value d_YYYYYYYYY not found"
As far as I know I have give the app the correct permissions.
Is there a limitation I am not aware of?
Am I doing something wrong?
Is there a special permission I have to set up for co-owned folders?
Thank you in advance !!
-
Are you using a token associated with your apps service account? If so, you need to ensure that the AutomationUser is added as a collaborator on the necessary folder so it can access it.
Best,
Kourtney
-
Hi
Your answer pointed me in the right direction, I was using the token associated with my service account when what I wanted was to use the one associated with the user account.
I needed to add some lines and the end result is the following (probably can be cleaned up and remove some redundancies)
def main(): user_id = "XXXXXXXXXX" sdk = JWTAuth.from_settings_file('key_config.json') client = Client(sdk) user = client.user(user_id=user_id).get() # NEW CODE auth_user = sdk.authenticate_user(user) sdk = JWTAuth.from_settings_file('key_config.json', access_token=auth_user) client = Client(sdk) items = client.folder(folder_id='0').get_items() for item in items: print('{0} {1} is named "{2}"'.format(item.type.capitalize(), item.id, item.name))
Thank you!
Please sign in to leave a comment.
Comments
2 comments