最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get Youtube video title with v3 URL API in javascript w Ajax & JSON - Stack Overflow

programmeradmin2浏览0评论

I am only trying to fetch Youtube video's title. Can't seem to figure it. So far I have this:

     q = '='+ itemId +'&key='+ ytApiKey +'&fields=items(snippet(channelId,title,categoryId))&part=snippet' ;

$.ajax({
      url: q, 
      dataType: "jsonp",
      success: function(data){
               alert(data.items[0].title);
               console.log(data.snippet.title);            
      },
      error: function(jqXHR, textStatus, errorThrown) {
          alert (textStatus, + ' | ' + errorThrown);
      }
  });

Thanks,

I am only trying to fetch Youtube video's title. Can't seem to figure it. So far I have this:

     q = 'https://www.googleapis.com/youtube/v3/videos?id='+ itemId +'&key='+ ytApiKey +'&fields=items(snippet(channelId,title,categoryId))&part=snippet' ;

$.ajax({
      url: q, 
      dataType: "jsonp",
      success: function(data){
               alert(data.items[0].title);
               console.log(data.snippet.title);            
      },
      error: function(jqXHR, textStatus, errorThrown) {
          alert (textStatus, + ' | ' + errorThrown);
      }
  });

Thanks,

Share Improve this question edited Jan 19, 2015 at 6:29 Kamy D asked Jan 19, 2015 at 6:06 Kamy DKamy D 1,1012 gold badges14 silver badges23 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 12

I got it working using

https://www.googleapis.com/youtube/v3/videos?id=itemId&key=apiKey&fields=items(snippet(title))&part=snippet

and

alert(data.items[0].snippet.title);

So, not much wrong with the syntax! But I found that the problem was really in the backend when setting up the Google API's 'allowed referers'. With V3 API, you can select which referers the API should belong to, so others cannot simply steal your API and use it. So the API will work if the request is originated from the domain name/IP you specify. When I don't give it restrictions, the code works, but when I do enter my domain it fails! I entered .mydomainname.com/ , the same format as it was suggested, but it errors out somehow.. Now I've got figure out why.

The following jquery code will fetch the title of the video.

$.ajax({
      url: "https://www.googleapis.com/youtube/v3/videos?id=" + videoId + "&key="+ apiKey + "&fields=items(snippet(title))&part=snippet", 
      dataType: "jsonp",
      success: function(data){
               console.log(data.items[0].snippet.title);           
      },
      error: function(jqXHR, textStatus, errorThrown) {
          alert (textStatus, + ' | ' + errorThrown);
      }
  });
发布评论

评论列表(0)

  1. 暂无评论