Youtube Data API | Set-1
Google provides a large set of API’s for the developer to choose from. Each and every service provided by Google has an associated API. Being on of them, Youtube Data API is very simple to use provides features like –
- Search for videos
- Handle videos like retrieve information about a video, insert a video, delete a video etc.
- Handle Subscriptions like lists all the subscriptions, insert or delete a subscription.
- Retrieve information about comments like replies to a specific comment identified by a parentId etc.
In this article, we will discuss Google Youtube API. Please follow the steps below to enable the API and start using it.
- Create New Project, Enable API and Create Credentials: In this step we will create a project and will enable the API.
- Go to Google Developers Console and Click on Sign In in the upper rightmost corner of the page. Sign In using the credentials of the valid Google Account. If you don’t have a google account, setup a account first and then use the details to Sign In on the Google Developers Homepage.
- Now navigate to the Developer Dashboard and create a new Project.
- Click on Enable API option.
- In the search field, search for Youtube Data API and select the Youtube Data API option that comes in the drop down list.
- You will be redirected to a screen that says information about the Youtube Data API, along with two options : ENABLE and TRY API
- Click on ENABLE option to get started with the API.
- In the sidebar under APIs & Services, select Credentials.
- In the Credentials tab, select the Create credentials drop-down list, and choose API key.
There are two types of Credentials: API Key and OAuth. OAuth provides you with Client Id and a Secret Key in the form of a .json file. OAuth is generally used where authorization is required like in the case of retrieving liked videos of a user. So for the rest cases where authorization is not required like searching for the videos using a keyword or for searching for the related videos etc we will be using API Key.
- Installation: Google API client for python can be installed using simple pip command:
pip install --upgrade google-api-python-client
Let’s start with the Search function.
There are five variants of search method – Search by Keyword, Search by Location, Search Live Events, Search Related Videos and Search My videos. Let’s cover first two types of search method.
Search by Keyword function: This will return the list of videos, channels and playlists according to the search query. By default, if the type parameter is skipped, method will display videos, channels and playlists. Default value of max results parameter is 5. This example retrieves results associated with the keyword “Geeksforgeeks”.
Search by Location function: This example retrieves results associated with the keyword “Geeksforgeeks”. The request retrieves top 5 results within 100km (specified by the
locationRadius parameter value) of the point specified by the location parameter value.
location parameter is a string that specifies Latitude/Longitude coordinates of a geographic location.
- location parameter identifies the point at the center of the area.
- locationRadius parameter specifies the maximum distance that the location associated with a video can be from that point for the video to still be included in the search results.
type argument can only be video in this search method type. In this example, we have used Latitude/ Longitude coordinates for Delhi, India.
Note: We have used only limited parameters in the above example. There are many other parameters that can be set and if not set then what default value they take can be found from Youtube Search List Documentation. Please refer to the documentation to have a full understanding of the available parameters.