Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
886 views
in Technique[技术] by (71.8m points)

javascript - gapi.client.youtube is undefined?

I am trying to use youtube api for javascript and am getting 'gapi.client.youtube is undefined'.

I have gone through the link: Why is 'gapi.client.youtube' from Youtube Data Api V3 undefined? , but couldn't get much help.

My code is placed below:

    <script>
         function load(){
          gapi.client.setApiKey('AIzaSyARvwirFktEIi_BTaKcCi9Ja-m3IEJYIRk');
          gapi.client.load('youtube', 'v3');
          searchA();
        //alert(gapi.client.youtube.channels);
    }

        function searchA() {
         var q = 'pink floyd';
         var request = gapi.client.youtube.channels.list({
             part: 'statistics',
             forUsername : 'GameSprout'
         });

         request.execute(function(response) {
         var str = JSON.stringify(response.result);
         alert(str);
     });

}

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

But its not getting gapi.client.youtube.

Can anyone please help me in his issue?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is that the gapi.client.load method takes a bit of time to complete, and it is asynchronous, so your page (which you've set up to be synchronous) is going on and running the searchA() method before the youtube library is fully loaded. There are two ways around this. One is to use the callback argument of the load method, like this:

<html>
<body>
 <script>
        function googleApiClientReady(){
                gapi.client.setApiKey('AIzaSyARvwirFktEIi_BTaKcCi9Ja-m3IEJYIRk');
                gapi.client.load('youtube', 'v3', function() {
                        searchA();
                });
        }
        function searchA() {
                var q = 'pink floyd';
                var request = gapi.client.youtube.channels.list({
                        part: 'statistics',
                        forUsername : 'GameSprout'
                });
                request.execute(function(response) {
                        var str = JSON.stringify(response.result);
                        alert(str);
                });
        }
 </script>

If you'd prefer, you could also wrap a promise around the loading callback:

<html>
<body>
    <script>
        googleApiClientReady=function() {
          loadApi() = function() {
                return new Promise(function(resolve,reject){
                        gapi.client.setApiKey('AIzaSyARvwirFktEIi_BTaKcCi9Ja-m3IEJYIRk');
                        gapi.client.load('youtube', 'v3', resolve);
                });
          };
          loadApi().then(function() {
                var q = 'pink floyd';
                var request = gapi.client.youtube.channels.list({
                        part: 'statistics',
                        forUsername : 'GameSprout'
                });
                request.execute(function(response) {
                        var str = JSON.stringify(response.result);
                        alert(str);
                });
          });
        };
</script>
<script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script>
</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...