从Youtube API文档中复制的示例不起作用



我正在尝试使用Youtube API搜索包含一些单词的视频,我已经在这个交互式应用程序上运行了我的示例https://developers.google.com/youtube/v3/docs/search/list?apix=true&apix_params=%7B%22部分%22%3A%5B%22片段%22%5D%2C%22q%22%3A%22ay%20vamos%22%7D。我已经复制了该页面上的源代码并删除了授权,因为我不需要它并填写了凭据。这是我在获取Uncaught TypeError: Cannot read property 'youtube' of undefined时一直出现的错误

<body>
<script src="https://apis.google.com/js/api.js"></script>
<script>
/**
* Sample JavaScript code for youtube.search.list
* See instructions for running APIs Explorer code samples locally:
* https://developers.google.com/explorer-help/guides/code_samples#javascript
*/
function loadClient() {
gapi.client.setApiKey("-------------------");
return gapi.client
.load("https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest")
.then(
function () {
return gapi.client.youtube.search
.list({
part: ["snippet"],
q: "ay vamos",
})
.then(
function (response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function (err) {
console.error("Execute error", err);
}
);
},
function (err) {
console.error("Error loading GAPI client for API", err);
}
);
}
// Make sure the client is loaded and sign-in is complete before calling this method.
</script>
<button onclick="loadClient()">execute</button>
</body>

"无法读取未定义"的属性"youtube";暗示CCD_ 2定义不正确。我自己运行了您的代码,并将gapi.client打印到控制台输出null

此外,我找不到任何调用loadClient()函数的东西,该函数看起来似乎负责实际加载youtube客户端?这可能是一个单独的问题。

最新更新