Youtube Data Api v3-未捕获的TypeError:无法读取未定义的属性'setApiKey'

问题描述 投票:1回答:1

我想使用YouTube数据API执行我的YouTube数据。

<!-- Google Sign in API JS -->
<script src="https://apis.google.com/js/api.js"></script>
<script>
    function onSuccess(googleUser) {
        console.log('Logged in as: ' + googleUser.getBasicProfile().getName());
        console.log('Logged in as: ' + googleUser.getBasicProfile().getEmail());
    }

    function onFailure(error) {
        console.log(error);
    }

    function renderButton() {
        gapi.signin2.render('my-signin2', {
            'scope': 'profile email',
            'width': 300,
            'height': 40,
            'longtitle': true,
            'theme': 'light',
            'onsuccess': onSuccess,
            'onfailure': onFailure
        });
    }
</script>
<script src="https://apis.google.com/js/platform.js?onload=renderButton" async defer></script>

<!-- Custom JS -->
<script src="./js/login.js"></script>
<script src="./js/logout.js"></script>
<script src="./js/scripts.js"></script>

Login.js

function authenticate() {
return gapi.auth2.getAuthInstance()
    .signIn({
        scope: 'https://www.googleapis.com/auth/youtube.readonly  https://www.googleapis.com/auth/youtube.force-ssl'
    })
    .then(function loadClient() {

            console.log('Sign-in successful');

            // If Log in successfull remove
            const sideLead = document.getElementById('side-lead')
            const rowContent1 = document.getElementById('rowContent1')
            sideLead.classList.add('d-none')
            rowContent1.classList.add('d-none')

            // then remove
            const sideNavs = document.getElementById('side-navs')
            const rowContent2 = document.getElementById('rowContent2')
            sideNavs.classList.remove('d-none')
            rowContent2.classList.remove('d-none')

            // Set API Key
            gapi.client.setApiKey("MY_API_KEY");
            return gapi.client.load("https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest")
                .then(function () {
                        console.log("GAPI client loaded for API");
                    },
                    function (err) {
                        console.error("Error loading GAPI client for API", err);
                    });

        },
        function (err) {
            console.error('Error signing in', err);
        });
}

此代码无效。 Chrome控制台显示:

未捕获的TypeError:无法读取未定义的属性'setApiKey'

javascript google-api youtube youtube-api youtube-data-api
1个回答
0
投票

尝试在HTML末尾添加此脚本

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.