Fabrizio has posted 1 posts at DZone. View Full User Profile

Getting Started With YouTube Java API

  • submit to reddit

In this tutorial I am taking a look at Google's YouTube API which allows you to empower your application with YouTube's features. YouTube is one of the “killer” Internet applications and its traffic comprises of a huge portion of the total internet traffic.

Before we get started, make sure you have read the API Overview Guide. We will mainly deal with the Data API, which allows you to perform many of the operations available on the YouTube website (search for videos, retrieve standard feeds, see related content etc.).

 The API is available in multiple programming languages and we will be using Java for this tutorial. Read the Java Developer's Guide to get a first idea. Also bookmark the Google Data API JavaDoc page.

 Let's prepare the development environment. First, download the GData Java Client from the corresponding download section. I will be using the 1.41.2 version for this tutorial. Note that there is also a version 2, but according to the site is experimental and not compatible with version 1. Extract the zipped file, locate the folder “gdata\java\lib” and include the following JARs into your application's classpath:

  • data-client-1.0.jar
  • gdata-youtube-2.0.jar
  • gdata-core-1.0.jar
  • gdata-media-1.0.jar
Next, we have to take care of the dependencies. The list of dependency packages can be found here.

Make sure all the aforementioned JAR files are included in the project's classpath.

Note that all public feeds are read-only and do not require any authentication. Authenticated operations, on the other hand, are those that include the retrieval of private feeds, such as a user's inbox feed, as well as write, upload, update and delete operations. You will need to sign up for a developer key to be able to execute authenticated operations. No key is required for this tutorial though.

Now that the infrastructure is ready, let's talk a bit about the API. The main class that we will be using is the YouTubeService class. This allows you to perform search queries similar to the ones that you can perform while browsing YouTube's web page. Each query is represented by a YouTubeQuery instance. The results of each query (if any), come in the form of a VideoFeed object. From each feed, a number of VideoEntry objects can be retrieved. From a VideoEntry, we extract a YoutTubeMediaGroup object. You can imagine this class as a placeholder for media information (check the "Media RSS" Specification). We then retrieve the corresponding MediaPlayer and finally the player URL. We can also get information about the accompanying thumbnails, via the MediaThumbnail class.

Let's get started with the code. First we create two model classes which will be used to hold information about the feeds and the videos. The first one is called YouTubeMedia and contains the media content URL and the media content type. The second one is named YouTubeVideo and contains all the information regarding a specific video (URL, embedded player URL, thumbnails and instances of YoutTubeMedia). The source code for these is following:

package com.javacodegeeks.youtube.model;

public class YouTubeMedia {

private String location;
private String type;

public YouTubeMedia(String location, String type) {
this.location = location;
this.type = type;

public String getLocation() {
return location;
public void setLocation(String location) {
this.location = location;

public String getType() {
return type;
public void setType(String type) {
this.type = type;

package com.javacodegeeks.youtube.model;

import java.util.List;

public class YouTubeVideo {

private List<String> thumbnails;
private List<YouTubeMedia> medias;
private String webPlayerUrl;
private String embeddedWebPlayerUrl;

public List<String> getThumbnails() {
return thumbnails;
public void setThumbnails(List<String> thumbnails) {
this.thumbnails = thumbnails;

public List<YouTubeMedia> getMedias() {
return medias;
public void setMedias(List<YouTubeMedia> medias) {
this.medias = medias;

public String getWebPlayerUrl() {
return webPlayerUrl;
public void setWebPlayerUrl(String webPlayerUrl) {
this.webPlayerUrl = webPlayerUrl;

public String getEmbeddedWebPlayerUrl() {
return embeddedWebPlayerUrl;
public void setEmbeddedWebPlayerUrl(String embeddedWebPlayerUrl) {
this.embeddedWebPlayerUrl = embeddedWebPlayerUrl;

public String retrieveHttpLocation() {
if (medias==null || medias.isEmpty()) {
return null;
for (YouTubeMedia media : medias) {
String location = media.getLocation();
if (location.startsWith("http")) {
return location;
return null;

Finally, the YouTubeManager class is presented. It can be used to perform search queries and returns instances of the YouTubeVideo model class with all the relevant information. It also creates the appropriate embedded web player URL. Here is the code for that class:
package com.javacodegeeks.youtube;

import java.net.URL;
import java.util.LinkedList;
import java.util.List;

import com.google.gdata.client.youtube.YouTubeQuery;
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.media.mediarss.MediaThumbnail;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.VideoFeed;
import com.google.gdata.data.youtube.YouTubeMediaContent;
import com.google.gdata.data.youtube.YouTubeMediaGroup;
import com.javacodegeeks.youtube.model.YouTubeMedia;
import com.javacodegeeks.youtube.model.YouTubeVideo;

public class YouTubeManager {

private static final String YOUTUBE_URL = "http://gdata.youtube.com/feeds/api/videos";
private static final String YOUTUBE_EMBEDDED_URL = "http://www.youtube.com/v/";

private String clientID;

public YouTubeManager(String clientID) {
this.clientID = clientID;

public List<YouTubeVideo> retrieveVideos(String textQuery, int maxResults, boolean filter, int timeout) throws Exception {

YouTubeService service = new YouTubeService(clientID);
service.setConnectTimeout(timeout); // millis
YouTubeQuery query = new YouTubeQuery(new URL(YOUTUBE_URL));


VideoFeed videoFeed = service.query(query, VideoFeed.class);
List<VideoEntry> videos = videoFeed.getEntries();

return convertVideos(videos);


private List<YouTubeVideo> convertVideos(List<VideoEntry> videos) {

List<YouTubeVideo> youtubeVideosList = new LinkedList<YouTubeVideo>();

for (VideoEntry videoEntry : videos) {

YouTubeVideo ytv = new YouTubeVideo();

YouTubeMediaGroup mediaGroup = videoEntry.getMediaGroup();
String webPlayerUrl = mediaGroup.getPlayer().getUrl();

String query = "?v=";
int index = webPlayerUrl.indexOf(query);

String embeddedWebPlayerUrl = webPlayerUrl.substring(index+query.length());
embeddedWebPlayerUrl = YOUTUBE_EMBEDDED_URL + embeddedWebPlayerUrl;

List<String> thumbnails = new LinkedList<String>();
for (MediaThumbnail mediaThumbnail : mediaGroup.getThumbnails()) {

List<YouTubeMedia> medias = new LinkedList<YouTubeMedia>();
for (YouTubeMediaContent mediaContent : mediaGroup.getYouTubeContents()) {
medias.add(new YouTubeMedia(mediaContent.getUrl(), mediaContent.getType()));



return youtubeVideosList;


In order to test our class, as well provide a show case example, we create the following simple test class:
package com.javacodegeeks.youtube.test;

import java.util.List;

import com.javacodegeeks.youtube.YouTubeManager;
import com.javacodegeeks.youtube.model.YouTubeVideo;

public class YouTubeTester {

public static void main(String[] args) throws Exception {

String clientID = "JavaCodeGeeks";
String textQuery = "java code";
int maxResults = 10;
boolean filter = true;
int timeout = 2000;

YouTubeManager ym = new YouTubeManager(clientID);

List<YouTubeVideo> videos = ym.retrieveVideos(textQuery, maxResults, filter, timeout);

for (YouTubeVideo youtubeVideo : videos) {
for (String thumbnail : youtubeVideo.getThumbnails()) {
System.out.println("\t" + thumbnail);



The Eclipse project for this tutorial, including the dependency libraries, can be downloaded here. Enjoy!

Brought to you by JavaCodeGeeks.

Published at DZone with permission of its author, Fabrizio Chami.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Mike P(Okidoky) replied on Thu, 2010/05/27 - 3:55pm

Question: is there enough functionality available in the API's that would allow me to write a utility that would help track my online "conversations" on the videos? The message threading, or lack thereof, have always sucked on Youtube. I'd like to see an interactive message thread hierarchy that clearly shows who's talking to who, and track when someone responds me. The utility would periodically scan and download messages, and would notify me when it discovers when someone responded to one of my messages. Youtube's built in notification often simply don't work (fails to notify) and doesn't notify if multiple people or multiple of my messages in one video were responded to. The utility would show the messages of concern all nicely highlighted. Can this be done? Do the API's have things like getMessages with meta info (when, ratings, etc)?

Loren Kratzke replied on Tue, 2010/06/01 - 9:37pm

Excellent post. I wish I would have read it the day it was published because the next day I set out learn exactly what is in your tutorial, the hard way. Ooops. But you have filled in the gaps for me. This is the best tutorial on the net right now in my opinion. Great work. Thank you very much.

David Yan replied on Wed, 2011/01/19 - 8:20am

I want port this project to Android, but failed, have you ever implement all these feature on Android? Thanks.

01-19 18:23:46.963: ERROR/dalvikvm(5369): Could not find class 'com.google.gdata.data.media.MediaStreamSource', referenced from method com.google.gdata.client.media.MediaService.getMediaResource
01-19 18:23:47.393: ERROR/dalvikvm(5369): Could not find class 'com.google.gdata.data.media.MediaMultipart', referenced from method com.google.gdata.client.media.MediaService.insert

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.