gapi.client.youtube 未定义? [英] gapi.client.youtube is undefined?

查看:19
本文介绍了gapi.client.youtube 未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 youtube api 用于 javascript 并且收到gapi.client.youtube 未定义".

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

我浏览了链接:为什么 Youtube Data Api V3 中的gapi.client.youtube"未定义?,但无法获得太多帮助.

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

我的代码放在下面:

    <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">    

但它没有得到 gapi.client.youtube.

But its not getting gapi.client.youtube.

有人可以帮我解决他的问题吗?

Can anyone please help me in his issue?

推荐答案

问题是 gapi.client.load 方法需要一点时间才能完成,而且是异步的,所以你的在 youtube 库完全加载之前,页面(您已设置为同步)正在运行并运行 searchA() 方法.有两种方法可以解决这个问题.一种是使用load方法的回调参数,像这样:

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>

这篇关于gapi.client.youtube 未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆