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.

client = GData::Client::DocList.new
client.authsub_token = params[:token]
session[:token] = client.auth_handler.upgrade()

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.

YouTube.update_video(params[:id], session[:token], params[:you_tube_entry])

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.

YouTube.delete_video(params[:id], session[:token])

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

s = YouTube.video_status(session[:token], params[:id])
@status = s.control.state.name

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.

Share this:

Privacy Preference Center