Youtube API查询返回null

时间:2013-12-31 23:44:22

标签: javascript youtube youtube-api google-api google-api-client

“Kanye West Amazing”的youtube搜索查询应返回多个youtube链接。但是,通过api的youtube查询返回null(来自console.log):

<!doctype html>
<html>
  <head>
    <title>Vidify</title>
  </head>

  <body>
    <!--Add a button for the user to click to initiate auth sequence -->
    <button id="authorize-button" style="visibility: hidden">Authorize</button>
    <script type="text/javascript">

      var clientId = '591751286145-ktngvf2s76uiuuevan6mo1fft0kchl8l.apps.googleusercontent.com';

      var apiKey = 'AIzaSyB0mteDV5vDKFR-iAv4Fx4OC2gp1Yhqe9U';

      var scopes = 'https://www.googleapis.com/auth/youtube';

      function handleClientLoad() {
        console.log('API key provided - authorizing client...');
        // Step 2: Reference the API key
        gapi.client.setApiKey(apiKey);
        gapi.auth.init(checkAuth);
      }

      function checkAuth() {
        console.log('entered checkAuth - authorizing...');
        setTimeout(function() { 
            console.log("requesting auth");
            gapi.auth.authorize({ 
                client_id: clientId, 
                scope: scopes, 
                response_type:'token',
                immediate: true 
            }, handleAuthResult); 
        }, 100); 
      }

      function handleAuthResult(authResult) {
        console.log('received authResult');
        var authorizeButton = document.getElementById('authorize-button');
        if (authResult && !authResult.error) {
          console.log("auth. successful");
          authorizeButton.style.visibility = 'hidden';
          makeApiCall();
        } else {
          console.log('auth failed.');
          authorizeButton.style.visibility = '';
          authorizeButton.onclick = handleAuthClick;
        }
      }

      function handleAuthClick(event) {
        // Step 3: get authorization to use private data
        console.log('retry');
        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
        return false;

      }

      // Load the API and make an API call.  Display the results on the screen.
        function makeApiCall() {
            console.log("loaded client");
            gapi.client.setApiKey(apiKey);
            // Step 4: Load the Google+ API
            gapi.client.load('youtube', 'v3', function() {
                console.log('youtube API loaded...');
                // Step 5: Assemble the API request
                var qVar = "Kanye West Amazing"
                // changed. added: type
                var request = gapi.client.youtube.search.list({
                    type: 'video',
                    part: 'id',
                    q: qVar
                });
                // Step 6: Execute the API request
                request.execute(function(resp) {

                    console.log(resp);
                    var vid = document.createElement('vid');
                    vid.value = resp.items[0].id.videoId;
                    console.log(vid.value);
                    var youtube_url = "http://www.youtube.com/watch?v=";
                    youtube_url += vid.value;
                    youtube_url += "&rel=0";
                    window.open(youtube_url);
                });
              });
          }
    </script>

    <script src="https://apis.google.com/js/client.js?onload=makeApiCall"></script>

  </body>
</html>

所以我实际上是尝试在spotify桌面应用程序中运行它。 spotify应用程序系统允许我从“spotify浏览器”中加载index.html。以下是我现在遇到的错误:

  

未捕获的TypeError:无法调用未定义的方法'setApiKey'

     

index.html:63 makeApiCall index.html:63(匿名函数)

     

client.js?onload = makeApiCall:6 fa client.js?onload = makeApiCall:1 b

     

client.js?onload = makeApiCall:6 v。(匿名函数)

     

client.js?onload = makeApiCall:6 H.(匿名函数)

     

client.js?onload = makeApiCall:6(匿名函数)cb = gapi.loaded_0:1

     

未捕获的TypeError:无法读取未定义的属性'prototype'   CB = gapi.loaded_0:6

     

_。J cb = gapi.loaded_0:6(匿名函数)cb = gapi.loaded_1:6(匿名函数)client.js?onload = makeApiCall:4 Z

     

client.js?onload = makeApiCall:6 ua client.js?onload = makeApiCall:4 E

     

client.js?onload = makeApiCall:5 b client.js?onload = makeApiCall:6

     

v。(匿名函数)client.js?onload = makeApiCall:6 H.(匿名   function)client.js?onload = makeApiCall:6(匿名函数)   CB = gapi.loaded_1:1

1 个答案:

答案 0 :(得分:1)

看起来你的嵌套可能不正确;您正在gapi.client.load函数的回调之外运行request.execute调用,因此在尝试运行时不会创建实际请求本身(gapi客户端异步加载API)。如果你试过这个:

  function makeApiCall() {
    // Step 4: Load the Google+ API
    gapi.client.load('youtube', 'v3', function() {
        console.log('youtube API loaded...');
        // Step 5: Assemble the API request
        var qVar = "Kanye West Amazing"
        // changed. added: type
        var request = gapi.client.youtube.search.list({
            type: 'video',
            part: 'id',
            q: qVar
        });
        // Step 6: Execute the API request
        request.execute(function(resp) {
          document.getElementById('vid').value = resp.items[1].id.videoId;
          console.log('saved video id successfully');
        });
      });
  }

然后它应该等待并在加载API代码后才执行请求。当然,这也假设你有一个id为“vid”的元素(你的示例代码没有)。

就范围而言,您不需要它们用于此特定呼叫,因为它不是需要进行身份验证的呼叫(即您可以在未经授权的情况下运行MakeApiCall())。但是,如果您稍后要做其他需要授权的事情,它会正常运行,因此您可以轻松地将其保留。