dreadnought303
dreadnought303

Reputation: 157

Fetching a video title from a YouTube URL - Java

I was trying to learn about how to work with the YouTube API using Java libraries, and was trying to do something basic like retrieving a video title given the video ID. However, all the information on this refers to the deprecated v2 API as explained in this link (https://developers.google.com/youtube/2.0/developers_guide_protocol_video_entries?csw=1) and I have not been able to find any sources online on how to accomplish this for the v3 API. If anyone could suggest some books/references on working with the YouTube API as well, I would be extremely grateful.

Upvotes: 1

Views: 3007

Answers (3)

Ravi Teja Patchipala
Ravi Teja Patchipala

Reputation: 28

There is a method called getTitle() in SearchResult, please check that once.

Upvotes: -1

Nefariis
Nefariis

Reputation: 3549

What you are looking for is .getTitle()

There are a few examples at https://developers.google.com/youtube/v3/code_samples/java?hl=es

for example:

private static void prettyPrint(Iterator<SearchResult> iteratorSearchResults, String query) {

System.out.println("\n=============================================================");
System.out.println(
    "   First " + NUMBER_OF_VIDEOS_RETURNED + " videos for search on \"" + query + "\".");
System.out.println("=============================================================\n");

if (!iteratorSearchResults.hasNext()) {
  System.out.println(" There aren't any results for your query.");
}

while (iteratorSearchResults.hasNext()) {

  SearchResult singleVideo = iteratorSearchResults.next();
  ResourceId rId = singleVideo.getId();

  // Double checks the kind is video.
  if (rId.getKind().equals("youtube#video")) {
    Thumbnail thumbnail = singleVideo.getSnippet().getThumbnails().get("default");

    System.out.println(" Video Id" + rId.getVideoId());
    System.out.println(" Title: " + singleVideo.getSnippet().getTitle());
    System.out.println(" Thumbnail: " + thumbnail.getUrl());
    System.out.println("\n-------------------------------------------------------------\n");
  }
}
}

Upvotes: 2

meyer1994
meyer1994

Reputation: 387

I am very neww to this API thing. But, isn't this part of the API that you need to achieve what you want?

snippet.title

I have never used Youtube's API. Please correct me if I am wrong.

Upvotes: 0

Related Questions