Click here to Skip to main content
15,867,986 members
Articles / Mobile Apps / Android

Youtube Data API v3 on Android – Introduction

Rate me:
Please Sign up or sign in to vote.
4.04/5 (9 votes)
24 Mar 2015CC (ASA 3U)3 min read 29K   8   5
Introduction to Youtube Data API v3 on Android

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:

JavaScript
{
   "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"
         }
      }
   ]
}
This article was originally posted at http://isaacrf.com/youtube-data-api-v3-introduction

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-Share Alike 3.0 Unported License


Written By
Software Developer (Senior) Connectis
Spain Spain
Software Developer, 2D/3D Design and game development enthusiast.

Good working on team or alone, very fast learning speed, curious and assertive, always learning new skills.

Comments and Discussions

 
GeneralMy vote of 5 Pin
JOE Heart Under Blade15-Apr-15 15:21
JOE Heart Under Blade15-Apr-15 15:21 
GeneralRe: My vote of 5 Pin
Isaac RF16-Apr-15 1:03
professionalIsaac RF16-Apr-15 1:03 
Thank you so much Joe! Your opinion and support are very appreciated ^^
"Analyze the problem calmly, and the solution will reveal itself"

-Isaac RF

GeneralRe: My vote of 5 Pin
JOE Heart Under Blade16-Apr-15 17:08
JOE Heart Under Blade16-Apr-15 17:08 
GeneralMy vote of 5! Pin
jediYL9-Apr-15 20:42
professionaljediYL9-Apr-15 20:42 
GeneralRe: My vote of 5! Pin
Isaac RF12-Apr-15 22:30
professionalIsaac RF12-Apr-15 22:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.