Using the Youtube API in Ruby
I recently embarked upon a project to create an application that allows people to create and share courses based upon already existing youtube content- as a way for autodidacts to better structure their autodidacticism and help others who wish to do the same. To do this, I needed to figure out how to get videos off of youtube, and into my application. I found this process rather opaque, and difficult to figure out, and figured I would write an article to help any others who may wish to venture this way in the future.
Setup: search and request.
Youtube has a robust api that can be accessed here. Functionally, this API is robust and intuitive, but getting to the point where you can easily make calls to this api is a different story, and depending on what you need, there are a few different ways of accessing information from the API. No matter how you intend to use the API, you need to start by obtaining an API key.
If you are creating an application that only wants to read from youtube(ie. you are not posting any content to the site), then you can get away with using a simple API key, you can do this from the credentials page of your google cloud developer account. If you are looking for more complex functionality, you need to set up Oauth credentialization which allows users to sign into their own google account from your site in order to upload content to youtube as themselves. I will not be covering this is this article, as I did not set it up for my application. Once you have your API key saved in a “.env” file in your root directory as a global ENV[“API_KEY”] variable, you’re ready to begin setup to read from the youtube API.
Yt Gem
I used the “YT gem” to help me access API content such as video information, details, and url links. I initialized the gem in an initializer file, which implements some behavior upon the initial boot of the application.
require 'yt'
puts ENV['YOUTUBE_API_KEY']
Yt.configure do |config|
config.api_key = ENV['YOUTUBE_API_KEY']
config.log_level = :debug
endr
Then I had to figure out how to access videos. This gem only works to access videos by id- the string of numbers and letters at the end of a youtube URL. It can then return all sorts of information about the video, its comments, its creator, and so on. I used this functionality to add videos to lessons in my application by their id. I gave my videos two seperate URL strings, “url” links to the video on youtube, and “embed_url” gives a link that I can embed in an “iFrame” html component in my react front end to make the video viewable on my own site.
ytvideo = Yt::Video.new id: id
video = lesson.videos.create(
url: "https://www.youtube.com/watch?v=#{id}",
embed_url: "https://www.youtube.com/embed/#{id}",
title: ytvideo.snippet.data["title"],
channel: ytvideo.snippet.data["channelTitle"],
description: ytvideo.snippet.data["description"],
thumbnail: ytvideo.snippet.data["thumbnails"]["medium"]["url"])
Search
One piece of functionality that this gem does not have, however, is searching youtube for a list of videos matching a search query which, as you can see here, is a feature of the API. In order to get the id’s for creating new videos with the Yt gem, i needed to find some way to allow people to search for videos from a simple text prompt from within my application. In order to implement this, I needed to make a basic call to the “search” endpoint of the youtube API using the “net-http” ruby gem. This allowed me to create a simple http request that returned a list of five videos related to the search term.
def search
key = ENV["YOUTUBE_API_KEY"]
search = params[:search]
uri = URI('https://www.googleapis.com/youtube/v3/search')
params = {:key => key, :part => 'snippet', :q=>search, :type=>'video'}
uri.query = URI.encode_www_form(params)
response = Net::HTTP.get_response(uri)
render json: response.body
end
I could then use this response to send the Id’s of any selected video back to the earlier route in order to save and store it as a video in my applicaiton.
After many hours I got both of these routes working on the backend, and successfully learned how to navigate the Youtube API.
Result
The result is an application that allows users to both search for videos and add them to any given lesson in their course.
I’m pretty proud of this application, and looking forward to continue building it into a full-fledged course website. I hope to use it to learn blockchain and ultimately allow users to credentialize their own understanding of self-taught knowledge on the blockchain through testing and sharing with others in a community of online learners.
I hope you found this information on the Youtube API helpful.
Happy Coding!