gpt4 book ai didi

javascript - YouTube数据Api v3-未捕获的TypeError:无法读取未定义的属性 'setApiKey'

转载 作者:行者123 更新时间:2023-12-03 05:34:40 26 4
gpt4 key购买 nike

我想使用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控制台显示:

Uncaught TypeError: Cannot read property 'setApiKey' of undefined

最佳答案

尝试在html末尾添加此脚本

<script type="text/javascript" src="https://apis.google.com/js/client.js?onload=init"></script>

关于javascript - YouTube数据Api v3-未捕获的TypeError:无法读取未定义的属性 'setApiKey',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60919024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com