Some time ago I wrote an article about Google Analytics API that interested you and was positively received. For this reason, I decided to write another article on this topic, today I will show you how to use YouTube API. Almost everyone knows and uses this website or mobile application. It is possible that you would like to manage your playlists or search for a video from another application. There is no problem with that thanks to this API. I will explain everything to you. Of course, the API is available for many technologies, but I will show you how to use it in .NET technology.
Some general information
YouTube API allows programmers to access video statistics or YouTube channel data and is part of the wide list of APIs that Google Cloud Platform offers us. You can also search for different videos, playlists or channel. Do you want to upload new material to your channel? You can use the YouTube API for this as well. It is worth mentioning that the use of this API is completely free.
YouTube API configuration
In the beginning, you should log in to Google Console Developers or create an account there if you do not already have one. Then create a new project:
Now complete the form below and go next.
Next step is finding the YouTube Data API v3 there and turn it on.
Go to ‘Credentials’ and there go to ‘Create Credentials’, select ‘Service account’.
Now it is time to complete the visible form, the description field is not required.
It is not required to complete the forms on two consecutive tabs, but I recommend creating a key in P12 format. The key file should automatically download, place it in the structure of your project so that you can refer to it later in the code.
The configuration part is behind us. I have already shown these few steps in the article about Google Analytics API, but I thought that it should also be found here so that you do not have to look for it. Now we can go to what developers like the most – that is, the code!
Communication of our application with YouTube
First, add the following libraries using NuGet Packages:
We will start the implementation with a code that will be responsible for communication of the application with YouTube and sending requests. At the beginning of the class, you can see the YouTube service declaration, which is necessary for implementation.
The next part concerns creating a constructor with two arguments. The first argument is the path to the key file, which you downloaded earlier when creating credentials. The second one is the account email address, which is also created while initializing a new project on Google APIs.
Inside the constructor are credentials that are necessary for our application to connect with YouTube. We can say that this is just an authorization. The next stage is the initialization of our website object, which is visible at the bottom of the listing.
Once we have made a connection to YouTube, we can now move on to writing the method responsible for sending requests to get specific data. This will be a sample request that will return random video from YouTube.
In this article, I wanted to show you how to start playing with the YouTube API and introduce you to what opportunities it provides. Using it is really pleasant and you can quickly add some features to your system that will use it. Other useful examples you can find on the official YouTube API website.