How to fetch file from Box using Python
Hi Platform and Dev community,
forgive me if this question seems redundant or has been asked, but I haven't seen it. So. I've just downloaded box on my desktop and I'm trying to figure out if there is a way to write a code in Python to fetch a file that will be dropped every day in Box.
for instance in ftp, we can do:
import ftplib
from ftplib import FTP
ftp(username,password)
Is there something similar I can do to fetch that file.
Thanks a lot
-
Hi ,
If you were to do this with the APIs, you would generally follow these steps:
- Set up a JWT application: https://developer.box.com/en/guides/applications/custom-apps/jwt-setup/
- Create a client with one of the SDKs: https://developer.box.com/en/guides/authentication/jwt/with-sdk/
- Make a call to download a file: https://developer.box.com/en/guides/downloads/file/
If you want to trigger that download process when the new file makes it into Box you can listen for a file upload event using the event stream: https://developer.box.com/en/guides/events/for-user/ (specifically ITEM_UPLOAD).
Those guides have samples in Python as well that should help.
- Jon
-
Hi Jon,
My use case is the following:
- I have a x number of files that I need to collect
- These files are owned by multiple owners
- I have the file id for each of the x files
Will the guides you listed help in doing so?
My app can have the maximum scope: will I need to use the "as-user" in my headers?
Thank you
Dimitris
-
Hi ,
Thanks for the context - the answer here depends on whether the users are all within the same enterprise or within different enterprises.
Before I do so, let me just mention that the ways to access a file that is owned by a user is to either:
- Make an API call on behalf of that user (As-User / user access token method for JWT, or the standard OAuth 2 flow).
- Collaborate the account making the API request (e.g. a service account) in on the file / folder so that they have access to it.
There is another method that involves some manual approval of a special scope but you won't need that for this use case.
Now let me break down both instances:
All users in the same enterprise
In this instance you will be using the JWT application type that I previously mentioned. When setting up the application, in the configuration panel, you would set the application access to enterprise, add the manage users scope, and toggle the "perform actions as users" or "Generate User access tokens" (or both) options on under the "Advanced Features" section. This will set up your application correctly to make calls within the entire enterprise on behalf of the users.
Now when your want to access a file for a user in the enterprise you will do one of three things:
- Generate a user access token for a user, then fetch the file owned by that user.
- Auth as the service account, then make an As-User call on behalf of that user to fetch the file.
- Collaborate the service account in on all files / folders you want access to. To fetch the files you will auth as the service account and simply make a call to fetch that file or folder. Collaborating them in will give the account access to the data.
Users are in different enterprises
If the users are spread across multiple enterprises your options will be more limited since you down own / have access to impersonate those users. Basically those users that own the content from another enterprise will have to collaborate your JWT app service account in to the files / folders that it would need access to. You can then make calls from the service account to fetch those files any time you need to.
Let me know if you have any other questions,
Jon
-
Jon
Thank you so much for the detailed response.
I think I need the "method that involves some manual approval of a special scope but you won't need that for this use case".
You see all the files are in the same enterprise instance. But, all I have is the files's Box ID - I do not know the owners's IDs to make the As-User calls (my app does have that scope). I need to be able to get the info for the file so I can get the owner's name and ID so I can make the call As-User.
Dimitris
サインインしてコメントを残してください。
コメント
4件のコメント