简单的youtube javascript api 3请求不起作用

时间:2013-08-07 15:24:46

标签: javascript youtube-api youtube-javascript-api

我试图用youtube javascript api v3编写一个简单的youtube请求来搜索视频。

这是源代码:

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript">

       function showResponse(response) {
           var responseString = JSON.stringify(response, '', 2);
           document.getElementById('response').innerHTML += responseString;
        }

       // Called automatically when JavaScript client library is loaded.
       function onClientLoad() {
          gapi.client.load('youtube', 'v3', onYouTubeApiLoad);
       }

       // Called automatically when YouTube API interface is loaded
      function onYouTubeApiLoad() {
        // This API key is intended for use only in this lesson.
        gapi.client.setApiKey('API_KEY');

        search();
       }

       function search() {
         var request = gapi.client.youtube.search.list({
             part: 'snippet',
             q:'U2'

        });

        // Send the request to the API server,
        // and invoke onSearchRepsonse() with the response.
        request.execute(onSearchResponse);
    }

    // Called automatically with the response of the YouTube API request.
    function onSearchResponse(response) {
        showResponse(response);
    }


    </script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script src="https://apis.google.com/js/client.js?onload=onClientLoad" type="text/javascript"></script>
</head>
<body>
    <pre id="response"></pre>
</body>
</html>

当我在谷歌浏览器上加载此页面(已更新)时,没有任何反应,页面仍为空白。 我已经为浏览器应用程序(带有引用程序)请求API密钥,并在方法gapi.client.setApiKey中复制。

任何人都可以帮助我?

由于

4 个答案:

答案 0 :(得分:2)

在这里试试这个例子

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google AJAX Search API Sample</title>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
    // How to search through a YouTube channel aka http://www.youtube.com/members

    google.load('search', '1');

    function OnLoad() {

      // create a search control
      var searchControl = new google.search.SearchControl();

      // So the results are expanded by default
      options = new google.search.SearcherOptions();
      options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);

      // Create a video searcher and add it to the control
      searchControl.addSearcher(new google.search.VideoSearch(), options);

      // Draw the control onto the page
      searchControl.draw(document.getElementById("content"));

      // Search
      searchControl.execute("U2");
    }

    google.setOnLoadCallback(OnLoad);
    </script>
  </head>
  <body style="font-family: Arial;border: 0 none;">
    <div id="content">Loading...</div>
  </body>
</html>

答案 1 :(得分:1)

当您使用&lt; script src =&#34; https://apis.google.com/js/client.js?onload = onClientLoad&#34; ..&GT;&LT; /脚本&GT;

你必须在网上某处上传html文件或在你的电脑上使用XAMPP

使用html搜索YT视频,在PC上使用Javascript,据我所知,我们需要使用其他编码:

1-在API 2.0版中使用与此类似的JavaScript代码。除了API KEY v3的存在。

2-使用jQuery方法&#34; $。get(..)&#34;为了这个目的。

请参阅: http://play-videos.url.ph/v3/search-50-videos.html

有关详细信息,请参阅(我的帖子&#34;用于搜索视频的JAVASCRIPT&#34;):

http://phanhung20.blogspot.com/2015_09_01_archive.html

&#13;
&#13;
var maxRes = 50; 
 function searchQ(){
      query = document.getElementById('queryText').value;
      email = 'https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=50'+
      '&order=viewCount&q='+ query + '&key=****YOUR API3 KEY*****'+
      '&callback=myPlan';
      var oldsearchS = document.getElementById('searchS');
      if(oldsearchS){
            oldsearchS.parentNode.removeChild(oldsearchS);
      }
      var s = document.createElement('script');
      s.setAttribute('src', email);
      s.setAttribute('id','searchS');
      s.setAttribute('type','text/javascript');
      document.getElementsByTagName('head')[0].appendChild(s);
  }
    
  function myPlan(response){
          for (var i=0; i<maxRes;i++){
          var videoID=response.items[i].id.videoId;
          if(typeof videoID != 'undefined'){    
          var title=response.items[i].snippet.title;
          var links = '<br><img src="http://img.youtube.com/vi/'+ videoID + 
                '/default.jpg" width="80" height="60">'+
                '<br>'+(i+1)+ '.&nbsp;<a href="#" onclick="playVid(\''+ videoID + 
                '\');return false;">'+ title + '</a><br>';
          document.getElementById('list1a').innerHTML += links ;
          }
      }        
 }
&#13;
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  </head>
  <body>
  <input type="text"  value="abba" id="queryText" size="80">&nbsp;
  <button type="button" onclick="searchQ()">Search 50 videos</button>
  <br><br>
  <div id='list1a' style="width:750px;height:300px;overflow:auto;
  text-align:left;background-color:#eee;line-height:150%;padding:10px">
  </div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

我必须从网络服务器发出请求。

感谢大家的回复

答案 3 :(得分:0)

我使用了Tom发布的原始代码,它给了我403访问权限错误。当我回到我的api控制台&amp;检查了我的api访问时间,它已过期。所以我重新创建了api的访问时间。它重建了新时代。并且代码在结果上运行良好。