Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get youtube video's title with jQuery using youtube api

What is the easiest way to get the title from the youtybe video , for example this video title :

http://www.youtube.com/watch?v=Wp7B81Kx66o

Thanks !

like image 266
AlexC Avatar asked Mar 19 '10 15:03

AlexC


People also ask

How do I get a YouTube video title from the YouTube API?

In order to make a request to the API, you can use jQuery's getJSON() call to request the title from YouTube via AJAX. YouTube's v3 Data API provides 3 endpoints that can be used to get the title: Snippet Title - The video's title.

Can you download videos using YouTube API?

Downloading Youtube videos is against their Terms of Service, so their API's will not support that.

What is snippet YouTube API?

snippet. object. The snippet object contains basic details about the video, such as its title, description, and category. snippet.

Is YouTube API a REST API?

The YouTube Application Programming Interface (YouTube API) allows developers to access video statistics and YouTube channels data via two types of calls, REST and XML-RPC.


2 Answers

Use jQuery's JSON call to the YouTube API to get the results back and then use jQuery to put the results where you want them. You can use firebug's NET tab to make sure you requests/respoonses are coming back correctly and then use console.log() to make sure you parsed the response correctly.

eg. URL:

GET https://gdata.youtube.com/feeds/api/videos/(the-video-id)?v=2&alt=json

More info:

YouTube API for a specific video

Developer's Guide: JSON / JavaScript

like image 79
easement Avatar answered Sep 20 '22 07:09

easement


This is a overhauled implementation of the original answer provided by @easement using the current v3 YouTube Data API.

In order to make a request to the API, you can use jQuery's getJSON() call to request the title from YouTube via AJAX. YouTube's v3 Data API provides 3 endpoints that can be used to get the title:

  1. Snippet Title - The video's title. The property value has a maximum length of 100 characters and may contain all valid UTF-8 characters except < and >.
  2. Snippet Localized Title - The localized video title, again with the maximum length described above
  3. Full Localized Title - The full length localized video title.

Sample Implementation using Snippet Title

var yt_api_key = {your YouTube api key},
  yt_video_id = {your YouTube video id},
  yt_snippet_endpoint = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id=" + yt_video_id + "&key=" + yt_api_key;

var jqxhr = $.getJSON(yt_snippet_endpoint)
  .done(function(data) {
    console.log("second success callback");
    var title = getTitle(data);
    // do something with title here
  })
  .fail(function() {
    console.log("error, see network tab for response details");
  });

function getTitle(snippet_json_data){
  var title = snippet_json_data.title;
  return title;
}

Debugging tip: You can use developer tools to view Network requests (i.e Chrome's developer tools or Firefox's Firebug) to make sure you requests/responses are coming back correctly and then use console.log() to log the returned data to make sure you parsed the response correctly.

Additional Reading: YouTube Data API "getting started"

like image 27
Sorry-Im-a-N00b Avatar answered Sep 21 '22 07:09

Sorry-Im-a-N00b