youtube data api v3 search

2019-06-09 02:28发布

I'm trying to use the keyword search from the youtube data api v3 on my weebly site, but I can't get it to work. I followed the instructions on the javascript code samples page. I've added the "auth.js" file and the "search.js" to my project and I've plugged my user id into the "auth.js" file. I inserted the following HTML code into my page, but it doesn't work. Is something wrong with the code? I tried changing the "disabled" attribute of the search button to "enabled", but it still didn't work. If it's not the code, is it perhaps because it's a weebly site? Any suggestions or help would be appreciated. Thanks.

<!doctype html>
<html>
<head>
<title>Search</title>
</head>
<body>
<div id="buttons">
  <label> <input id="query" value='cats' type="text"/><button id="search-button"    
disabled onclick="search()">Search</button></label>
</div>
<div id="search-container">
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="auth.js"></script>
<script src="search.js"></script>
<script src="https://apis.google.com/js/client.js?onload=googleApiClientReady">
</script>
</body>
</html>

2条回答
混吃等死
2楼-- · 2019-06-09 03:10

Here's one way:

<!doctype html>
<html>
  <head>
  <title>Search</title>
  </head>
  <body>
    <div id="buttons">
    <label> <input id="query" value='cats' type="text"/><button id="search-button"   onclick="keyWordsearch()">Search</button></label>    
    <div id="container">
      <h1>Search Results</h1>
      <ul id="results"></ul>
    </div>           
    <script>
     function keyWordsearch(){
        gapi.client.setApiKey('api_key_here');
        gapi.client.load('youtube', 'v3', function() {
                makeRequest();
        });
}
    function makeRequest() {
        var q = $('#query').val();
        var request = gapi.client.youtube.search.list({
                q: q,
                part: 'snippet', 
                maxResults: 10
        });
        request.execute(function(response)  {                                                                                    
                $('#results').empty()
                var srchItems = response.result.items;                      
                $.each(srchItems, function(index, item) {
                vidTitle = item.snippet.title;  
                vidThumburl =  item.snippet.thumbnails.default.url;                 
                vidThumbimg = '<pre><img id="thumb" src="'+vidThumburl+'" alt="No  Image Available." style="width:204px;height:128px"></pre>';                   

                $('#results').append('<pre>' + vidTitle + vidThumbimg +  '</pre>');                      
        })  
    })  
}
  </script> 
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  <script src="https://apis.google.com/js/client.js?onload=googleApiClientReady">  </script>
</body>
</html>
查看更多
甜甜的少女心
3楼-- · 2019-06-09 03:29

You probably a number of issues here. Have you registered your host with the Google App Console for example https://console.developers.google.com/?

To explicitly test search, you can use the online tool in their documentation at https://developers.google.com/youtube/v3/docs/search/list#try-it

Maybe you might refine your problem space and resubmit your question?

查看更多
登录 后发表回答