Upload Folders and Files using API
We have a batch process that runs every night on an application server. At the end of the process many reports are created for the day and put into a folder for that day.in the Reports folder.
Reports\Monday
Reports\Tuesday
Reports\Wednesday
Reports\Thursday
Reports\Friday
In the past the solution was to copy the files to the user workstation that this reports are for (executive level person) and Box Sync would move the files to Box. If for whatever reason the machine wasnt left logged in then the sync would fail. We never liked this solution, but the organization wouldnt give us rights to create a box application to move the files. Now the organization has been spun off and we can do this.
Want to do it right. The batch process is written in .NET and we want to add a step at the end that connects to Box and uploads all the files. Wouldnt think this is a odd solution need.
So setup app with what I think is proper configuration
OAuth 2.0 with JWT (Server Authentication)
Developer Token
OAuth 2.0 Credentials (Client ID, Client Secret)
Enterprise Application Access
Application Scopes: Read and write file selections and nothing more
Generated keys and downloaded JSON config
Questions: Does that configuration sound correct? This will run from a Windows Service not an application a user uses.
Is there a way to upload an individual folder and all its contents or do we need to upload file by file?
How do you use the Developer Token?
Is there any Box Add-In or app that already does this and we shouldnt develop ourselves?
-
great to see you moving forward with this!
Firstly, it's worth learning about the different user types. Especially look at the Service Account which is what a JWT application authenticates as. https://developer.box.com/en/guides/authentication/user-types/
Secondly, the developer token won't get you far as it's only valid for 1 hour and can't be renewed.https://developer.box.com/en/guides/authentication/access-tokens/developer-tokens/
Instead we recommend using our .NET SDK to get a new access token every time. All of this can be found here.
https://developer.box.com/en/guides/authentication/jwt/with-sdk/
Finally, we have some (old-ish) sample code on uploading a whole folder to Box. Have a look at this. https://developer.box.com/en/guides/uploads/chunked/folder/
This does make multiple API calls, which is also what Box Sync would do under the hood. Our only tool that supports uploads of folders is the Command Line Interface (CLI). https://github.com/box/boxcli/blob/master/docs/folders.md#box-foldersupload-path
-
First off our administrator had approved the app in the admin tool. Dont know if he has to somehow say the app has rights to upload files. Then dont know if users have to give app rights somehow to specific folders.
I am further along, but getting a Not Found when try to upload a file. I have seen some other posts about this error. What I think it is is that the Service Account doesnt have rights to the folder I am trying to upload to.
The code
Creates BoxConfig from Json file
Creates BoxJWTAuth from the BoxConfig
Creates AdminToken using BoxJWTAuth
Tries to Upload file to existing folder. I retrieved the folder ID from the browser. Is there some step missing or am I using wrong object.
-
Yes, the service account is a whole different user. Please read the User Types guide I shared with you.
Using JWT, to access folders on behalf of another user, you need to either use the `As-User` header:
https://developer.box.com/en/guides/authentication/jwt/as-user/
Or you need to create a User Access Token
https://developer.box.com/en/guides/authentication/jwt/user-access-tokens/
Alternatively, you can make a one-off API call with a developer token for the user who owns the folder to add the service account as a collaborator on the folder.
https://developer.box.com/en/guides/collaborations/share-folder/
Please sign in to leave a comment.
Comments
3 comments