Ruby on Rails App: Uploading files using box
What is box?
Box is a cloud-based service that allows users to securely store, access, share, and collaborate on files across devices.
Box also offers an open platform with APIs and SDKs, known as the Box Platform, that developers can use for a variety of purposes.
Our main purpose is to upload a file in Box using boxr gem.
How to integrate box?
First of all, we need to create one account and an application on Box platform.
Sign up for a Box Developer Account.
Create a new application by clicking My Apps from left side panel in developers console.
Select Standard OAuth 2.0 (User Authentication) for the new application.
After creating the application in box account, it will redirect you to application’s configuration page having Developer Token, Client ID and Client Secret. For one time authentication of our application, we need to set Redirect URI. This is the URL of our Rails Application (Must be secure). Set URI like,
Now, install boxr gem in our rails application and create one table to store access token and refresh token of a box for future use as box’s developer token (access token) expires in an hour.
In Rails console, run the following commands.
BOX_CLIENT_ID=YOUR CLIENT ID
BOX_CLIENT_SECRET=YOUR CLIENT SECRET
oauth_url = Boxr::oauth_url(URI.encode_www_form_component('1233'))
Now redirect to
oauth_url (pass client Id at the end of url if not present). And click on
Grant Access button, it will redirect to your
Below is the code snippet of token_catcher method.
Now, If you want
Boxr to automatically refresh the tokens once the access token becomes invalid you can supply a refresh token, along with your
client_secret, and a block that will get invoked when the refresh occurs.
To upload a file, first, we create one folder in box to store it, for example, images folder in our box application.
The above code will upload a file to the specified folder and will provide the public link for the same. You can control access by specifying different options. I will cover that in my next blog.
That’s it! Have a happy coding.
At BoTree Technologies, we build enterprise applications with our RoR team of 25+ engineers.
Consulting is free – let us help you grow!
Choose Your Language
Subscribe to Our Newsletter
- Digital Marketing
- IT Consulting
- Project Management