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:
- Use OAuth
- 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 https://console.developers.google.com/ 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!
Ciao,
Marco.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# Load the necessary packages require(curl) require(jsonlite) # Your API key obtained via https://console.developers.google.com/ API_key=‘<paste_your_API_key_here>’ # Base URL for Google API’s services and YouTube specific API’s Base_URL=‘https://www.googleapis.com/youtube/v3’ # YouTube Web Services # Note that we have replaced the %2C with “,” so sprintf works correctly with it # as an alternative we can add an extra % in front of %2C to make it %%2C YT_Service <– c( ‘search?part=snippet&q=%s&type=%s&key=%s’, # search API ‘subscriptions?part=snippet,contentDetails&channelId=%s&key=%s’ # subscriptions API ) # Form request URL # channelId=UCAuUUnT6oDeKwE6v1NGQxug is the TED channel, used here as an example url <– paste0(Base_URL, “/”, sprintf(YT_Service[2], ‘UCAuUUnT6oDeKwE6v1NGQxug’, API_key)) # Perform query result <– fromJSON(txt=url) # Print the title of the channels this channel has subscribed to result$items$snippet$title # Print the Id’s of the channels this channel has subscribed to result$items$snippet$resourceId$channelId |
4 responses to “Accessing the YouTube API v3 with R”
Hi,
I am trying to do the same, the above code runs fine but when i Try to access the analytics api with the same credentials it gives
Any help?
Hi,
did you enable the Reporting and Analytics API inside the Google API Manager? They are different from the Data API and require to be specifically enabled and authorised. Read the instructions on Google’s Developers site, they are pretty straightforward. Once done your code should go past the authorisation and work as expected.
Cheers,
Marco.
I have enabled them, and I am able to access the from the google api reports builder. But now when I call the url, it still gives the same error. I rechecked the keys and tried through Oauth too but the api response i get is the same
Sorry Tabu, it is difficult for me to help on the basis of such limited information. Too many possibilities.
Try to narrow down the problem to either your credentials having something wrong with them or the way you call the API service. That’s all I am able to recommend you right now.