Accessing the YouTube API v3 with R

This is a quick tutorial on how to access the YouTube API v3 from within R.

A possible scenario is that you would like to perform an analysis of how channels are related to each others or query YouTube to find out the channels that are related to a certain keyword.

Step 1

In order to access the YouTube API v3 you first need to be authorised. There are two ways to proceed:

  1. Use OAuth
  2. Get an API Key

For the sake of simplicity we will work with method 2, even though 1 is also possible in R (see for example the httr package).

You should now go on and obtain your API Key at I have masked my own API Key in the following code for security reasons and also because each key corresponds to a quota (maximum daily use of the YouTube API’s) that should not be exceeded.

Step 2

You are basically all set. Here is a sample code on how to perform a query to YouTube using the API’s. Replace the API_key with the one obtained from Google in Step 1.

The fromJSON function performs the query and parses the results, which are originally in JSON format, into a nested list which can be further manipulated in R.

Other available YouTube API services use pretty much the same format. See for example the search service within YT_Service[1]. For more information on the what you can do with the YouTube API’s, consult the online documentation starting here.

One final element you need to pay attention to. Results from the API query are paginated. You can set the number of results per page via the MaxResults parameter, which defaults to 5 and has a maximum value of 50 in the current API v3. To get the next page results (assuming MaxResults is exceeded), you will need to repeat the query adding the pageToken parameter and setting it to be equal to nextPageToken as returned by the previous query call.

Happy YouTubing with R!



Continue Reading

YouTube video doesn’t play on iPad

If your YouTube video fails to play on the iPad with the message “This movie format is not supported”, this may depend on its copyright status.

Copyrighted videos, containing for example a copyrighted sound-track, will not play from the iPad YouTube application, popping up the totally misleading message about the unsupported video format.

The truth is the the video format is usually perfectly fine. The problem is more likely related to the fact that YouTube cannot advertise within the iPad application or is unable to respect some contractual obligations toward the copyright owners.

You can check the copyright status of your videos from the “My Account – Videos” page on YouTube. If it says Video blocked in some countries. View Copyright Info. then your video may not play on the iPad, even from those countries for which it doesn’t seem to be blocked.

YouTube alternatives, like Vimeo, do not show the same behavior.

Hope this helps to clarify the issue many of us are having with their YouTube videos on iPad, even within the boundaries of the fair use.

Continue Reading