Now You Can Have Better Youtube Integration with Ruby on Rails

I recently wanted to upload, edit, update and delete a video directly on youtube from my rails application. This can be achieved by YouTube Data API that allows application to perform functions normally executed on youtube.

Step 1 : Demystifying Authentication with Google AuthSub

Google provides an authentication service to those web applications that need to access services protected by a user's Google account. I chose Google’s ‘AuthSub’ scheme for this. This also provides a high level of security to web application.
The AuthSub interface provides several methods for acquiring and managing authorization tokens. Once a web application has received a token, it can request access to a Google service.

To obtain an authentication token, submit an HTTP POST request to the following URL:

Setting session parameter to 1 indicates that the single-use token can be exchanged for a session token.
AuthSubSessionToken is a method that allows the web application to exchange a single-use token for a session token. I used a gem ‘gdata’ to convert the token into a session token.

Session token allows the application to make unlimited calls to the Google service. When using session tokens, your application should store the session token for each user rather than requesting a new one each time it needs to access a Google service. For other token management options, see Working With AuthSub.

Step 2 : Getting the Youtube Developer Key

A developer key is required that identifies the YouTube developer that is submitting an API request. A client ID identifies your application for logging and debugging purposes. Please visit here to obtain a developer key and client ID. https://developers.google.com/youtube/v3/

Step 3 : Improving the Youtube Model Plugin

“youtube-model” is a plugin  that provides youtube video uploading functionality. That was a good start.
I forked the plugin and updated it with few methods to get the rest of  the functionality done. Each method is explained here.
1. uploaded_by_user
A method uploaded_by was provided in the plugin to retrieve all the videos of the user but it receives username as a parameter. So I added a method uploaded_by_user that receives the session token and solves our purpose.

2. update_video
The video can be updated by using this method passing video id, session token and params hash as parameters.

3. delete_video
The video can also be deleted if uploaded at the user’s youtube account by passing video id and token as a parameter.

4. video_status
This method helps finding out the status of uploaded video.

The changes to the plugin have been merged in the master and you can download everything here. I will really appreciate your feedback and suggestions to make this plugin better.

Vibha Chadha is working with Vinsol as a rails developer. When she is not writing code, she likes to read "geek" books and listen  to some "good" music.


Ever tried Rails.root

Guys, have you ever tried Rails.root. It gives same value as RAILS_ROOT. But now in edge rails it is modified and you can do much more with it.

You can use Rails.root at places like:

as

Both gives the same result.


Rails-Tip : Use Capture to wrap a part of View in a variable

The view helper method capture provides this functionality to wrap a section of the view template in a variable and can be used multiple times in the view.

Usage :

Advantage:

If its needed to render a section of template code, we usually go with partials. Capture is specifically useful when partial is only going to be used in just one view template. As there will be this variable available throughout the template, we can use it as many times as needed instead of partial.