What Is API and How To Use Youtube API | by Jiwon Jeong | Towards Data Science

Explaining the concept of API in plain English and how to scrape Youtube data using tuber package
Last time I talked about strategies for a successful Youtuber. It was such an exciting project for me. But It wasn’t an easy one because I had to struggle with searching for how to get Youtube API key. There are lots of resources already but most of them are for web developers, and tutorials for R users are hardly seeable. After I managed to get the API key spending a whole day, I came across with the idea why not I leave a post for someone who would have the same problem with me.
Today I’m going to talk about the concept of API and how to get API key. I will also introduce tuber package for analyzing data in R. If you haven’t used Google APIs and you are interested in doing a data science project with them, this post will be a good start.
If you are unfamiliar with the word API, it could be hard to grasp the meaning of it at first. API stands for Application Programming Interface. Is this too ‘programmer’ language for you? That’s fine. I will explain the concept step by step.
For your understanding, it would be better to start with two different interfaces. There are two kinds of interface, User Interface and Application Programming Interface. To simply put, UI is the interface for common users while API is the interface for programmers. You can see an example of UI below. It’s a common web page we encounter every day. How do we use them? We can use them simply by clicking the picture or words. This is the level for the common people, the interface for users. How could it be possible? That’s because the web developer hid all the codes required behind the button. And that is the interface for programmers where the codes and commands are living. In other words, API. You can think API as a set of codes, protocols, and functions to work and interact with software applications. With UI we work with simple buttons and tools while with API we work with predefined functions.
Still hard for you? Then let’s suppose you are sitting in a restaurant. You want to have a nice beef steak and a glass of red wine for your dinner. You sit on the table and call.. who? The waiters! Then you get the menu and order what you want. After a few minutes later they will bring the dish. This is what API does. You don’t need to roll up your sleeves and do all the stuff like preparing the ingredients, cutting the vegetables, and grilling the meat by yourself. Waiters will do these steps for you, and all you have to do is just calling them and start your work from that stand.
Likewise when the developers are working with programming tools or when the data scientists are wrangling with data, they don’t write every single code every single time. They use pre-made functions which are the sets of those basic codes operating under the hood. For instance, when you want to join two data into one, you call the join function (a ‘join’ waiter) and order it to work as you command (ordering the menu). Then it will return the required results (your dish).
API now became a new kind of business models and strategies for companies to equip in the big data era. Then what’s so special? What makes API so hot these days? Let’s briefly talk about what we can do with it in a business context.
APIs can generate massive amounts of value both internally and eternally. Managing and processing data is one of the crucial factors in business management, and every company has built IT systems. As the size of the data is increasing exponentially, however, there is a limitation to cope with all the data through the traditional IT system. In this sense, applying API can be a solution with better efficiency and security. It can break down barriers between systems, which enables simplifying work processes, inter-cooperation between organizations and higher protection of data.
External merits of API are even fancier. If a company open their API either publicly or with extra fees, it can provide new services and acquire potential customers to their side. Customers can experience a higher level of services that weren’t available before. By offering API services, third-party developers can build whole new kinds of products that even the companies had never thought of. For example, Google Map, the most popular API among developers, wasn’t expected to draw that much effects at first. By Applying those data to real estate and various other fields, developers brought much higher values and assets back to Google.
Nowadays the number of API is continuously increasing, and this trend will be ongoing or even more. Now we can say that the implementation and management of API of a company is one of the critical factors for its competitive and strategic values.
To work with API, you need to get an authorized key first. It’s for obtaining an authorized key to be connected with API. API Providers, Youtube in this case, don’t merely provide their service without control. To preserve this interface and manage the users, they offer a unique access key to each user. With this, we can connect to the interface of the application. Just like we connect to electricity by plugging the power cord into an electric outlet, we plug our server into the storage with this unique key. And from that point, we can use the data and protocols of API.
So now let’s start with how to get the authorized key. There is a very nice tutorial for getting the key. If you’re a web developer, you can follow this tutorial step by step.
help.aolonnetwork.com
If you are to do some data analysis, you don’t need all these steps. You follow the steps until number 9, and then choose ‘other’ instead of ‘web application.’ There are two different types of clients on the developer console, and the access methods are different for each case. Therefore if you try to get the key by ‘web application’ and ask for Oauth from your local computer, you could meet the error message like what I got.
So click ‘other’ option then you will get the client key and secret key. Copy and paste them on your local computer.
In R, there is tuber package which is made only for Youtube data analysis. You can draw various data including the number of videos of a particular channel, the number of views, likes and comments on a video and so on. To call this data in R, you need to install ‘tuber’ package first. Then you ask Google for the authorization token.
install.packages('tuber')library(tuber)client_key = ''client_secret = ''yt_oauth(app_id = client_key, app_secret = client_secret)
When you comment this code, a browser will pop up with some numbers allowing you to authorize the application. If you check your local tool, R will already be waiting for the numbers so copy and Paste the serial numbers. Now you finally get to be connected with Youtube API.
I will also introduce some of the functions that could be useful for analyzing Youtube data from tuber package.
get_all_channel_video_stats : This is the function for getting statistics on all the videos in a channel. You need a channel_id to use this.
get_stats : This gives you statistics of a video with video_id. The return values are the count of views, likes, dislikes, favorites, and comments.
get_video_details : This provides more specific information as the publish date of a video. You can also get titles, descriptions, thumbnails, and categories.
get_all_comments : It gives you all the comments for a video so has great usage for text mining in respect of particular topics or channels
list_channel_activities : This returns a list of channel activity.
list_videos : It returns the most popular videos.
There are other useful API calls, so I recommend you to check them. You can browse them from here.
An excellent video for what is API: https://www.youtube.com/watch?v=s7wmiS2mSXY&t=75s
Harvard report on strategic values of API: https://hbr.org/2015/01/the-strategic-value-of-apis
Want to study more about API? ProgrammableWeb is an ‘API’ university delivering almost everything about API: https://www.programmableweb.com/
Don’t know what to do with Youtube data? Check out my previous work. You could get new inspirations from them!
towardsdatascience.com
towardsdatascience.com
There are plenty of APIs already but, a small number of them open publicly. As data will directly transfer to the competitiveness in today’s world, I do understand companies’ intentions. I believe, however, they shouldn’t neglect the potentials of sharing economy. By making their internal data accessible, they will unleash the creativity of developers and data scientists all over the world to devise whole new uses for the data. Just like what we have done with open sources over the last years.
Thank you for reading and hope you found this post helpful. If there is something need to be corrected, please share your insight! If you’d like to encourage an aspiring data scientist, please hit 👏 👏 👏! I’m always open to hearing your thoughts so feel free to share or contact me on LinkedIn. I’ll be back with another exciting story. Until then, happy machine learning.