Youtube Data API v3 on Android – Introduction - CodeProject

:

First Things First

If you have ever developed an app that needs to obtain information from YouTube channels or videos, you probably noticed that querying the YouTube Data API v3 isn't that simple, and can cause more than a headache at the beginning.

In order to facilitate the use of this API, I'm going to write a series of articles with the basics, so you can understand how the queries work and make your own app in no time, starting from this post, that is a brief introduction.

Youtube Data API v3, What Is It?

Youtube logo

YouTube offers a public URL (https://www.googleapis.com/youtube/v3), known as Data API, so applications, websites, etc. can read the public information from YouTube channels and videos (also read / write private information, loging first in the OAuth System), and at the same time, it has an Android (and other OS) specific APIs, that allows you to generate, without effort, video players, preview images, layouts, etc. in your APP.

Analyzing the previous versions of the Data API, v3 has been restructured to recover only the specific pieces of information needed, instead of all the channel / video data, avoiding an enormous consumption of server resources for YouTube, and a waste of time for you, waiting for all the data to be retrieved, and the consequent post-processing of the data to get just what you need and discard the rest.

The basic way to communicate with YouTube Data API v3 is trough HTTP queries to the public URL. Google also offers a website called the Youtube API Explorer, that allows you to make test queries to the Data API specifying the info you want to retrieve: https://developers.google.com/apis-explorer/#p/youtube/v3/.

HTTP Request Diagram

The problem we face here is that Android API is not intended to recover information, but to show it, and don't expose direct methods to access the Data API, so we have to manually execute HTTP queries to the public URL sending different GET parameters (See also Query Strings article on Wikipedia the understand URL parameters) depending on the information we want to retrieve, a process that is not simple and requires a certain level of knowledge of the platform / language we are working with.

Before Finishing, A Simple Example

I don't want to extend the introduction much more, let's see a small example using the API explorer website from Google.

If we access the following URL and press the Execute button, we can obtain the basic data from Murata Shinya's YouTube channel, please take a moment to look at the form to see the parameters I chose and how I filled them out, the data retrieved will be useful so we can later obtain, for example, the video list of the channel:

 

https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.channels.list?part=contentDetails&forUsername=muratashinya&_h=2&

The real URL you would use in a manual HTTP query to obtain that information, is this one:

 

https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername=muratashinya&key={YOUR_API_KEY}

Let's analyze briefly the parameters used in the above URL:

 

"part=contentDetails" : Specifies the "piece" of information we want to retrieve, "contentDetails" asks for just the details of the element, we could fill the part parameter with other options such as "id" (Retrieves just the channel ID), snippet (Title and description), among others like "statistics", "topicDetails", or "invideoPromotion".
"forUsername=muratashinya" : User / Channel name we are going to retrieve the information piece specified in the part parameter. If we had the Channel ID instead of the user name, it should be specified in the id parameter and not in this one.
"key={YOUR_API_KEY}" : You should fill this parameter with the API key we obtain from the Google APIs console (We will cover this point in the next article) so we can query the Data API URL without restrictions. If you don't specify this parameter, you probably won't be able to query the URL and you will get a "Forbidden" error.

Making a query with these parameters will return us the following information in JSON format, please take a look at the important parts in the lines highlighted:

{
   "kind":"youtube#channelListResponse",
   "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/o5v09H65zuc-XUWE5X_6BwBIZ8U\"",
   "pageInfo":{
      "totalResults":1,
      "resultsPerPage":5
   },
   "items":[
      {
         "kind":"youtube#channel",
         "etag":"\"F9iA7pnxqNgrkOutjQAa9F2k8HY/FIvqS4LrMW8rCgf7dBV3pV3lcJw\"",
         "id":"UCwsU7gbEim-JhbgeUvfgLqg",		//ID del canal de youtube
         "contentDetails":{
            //Related playlists IDs, the list of videos liked, faved and uploaded by the user
            "relatedPlaylists":{
               "likes":"LLwsU7gbEim-JhbgeUvfgLqg",	//Liked
               "favorites":"FLwsU7gbEim-JhbgeUvfgLqg",	//Faved
               "uploads":"UUwsU7gbEim-JhbgeUvfgLqg"	//Uploaded
            },
            "googlePlusUserId":"105248226013511133334"
         }
      }
   ]
}