Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

File upload with Box using Ruby On Rails

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.

  1. Sign up for a Box Developer Account.

2. Create new application by clicking My Apps from left side panel in developers console.

3. Select Standard OAuth 2.0(User Authentication) for new application.

After creating 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,

https://90e9452e.ngrok.io/token_catcher

Now, install boxr gem in our rails application and create one table to store access token and refresh token of box for future usage 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
require 'boxr'
require 'dotenv'
Dotenv.load("./.envoauth")
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 token_catcher method.

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_id and 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 file to the specified folder and will provide 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, we build web and mobile applications to add value to our client’s business. We align ourselves to ensure that our client benefits the most out of our engagement.

We work in Ruby on Rails, Python, Java, React, Android and iOS.

Drop us a line to discuss how can we help take your business to the next level.


File upload with Box using Ruby On Rails was originally published in BoTree Technologies on Medium, where people are continuing the conversation by highlighting and responding to this story.



This post first appeared on BoTreeTechnologies Official, please read the originial post: here

Share the post

File upload with Box using Ruby On Rails

×

Subscribe to Botreetechnologies Official

Get updates delivered right to your inbox!

Thank you for your subscription

×