【发布时间】:2013-08-07 15:24:45
【问题描述】:
我尝试编写一个简单的 youtube 请求来使用 youtube javascript api v3 搜索视频。
这是源代码:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function showResponse(response) {
var responseString = JSON.stringify(response, '', 2);
document.getElementById('response').innerHTML += responseString;
}
// Called automatically when JavaScript client library is loaded.
function onClientLoad() {
gapi.client.load('youtube', 'v3', onYouTubeApiLoad);
}
// Called automatically when YouTube API interface is loaded
function onYouTubeApiLoad() {
// This API key is intended for use only in this lesson.
gapi.client.setApiKey('API_KEY');
search();
}
function search() {
var request = gapi.client.youtube.search.list({
part: 'snippet',
q:'U2'
});
// Send the request to the API server,
// and invoke onSearchRepsonse() with the response.
request.execute(onSearchResponse);
}
// Called automatically with the response of the YouTube API request.
function onSearchResponse(response) {
showResponse(response);
}
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="https://apis.google.com/js/client.js?onload=onClientLoad" type="text/javascript"></script>
</head>
<body>
<pre id="response"></pre>
</body>
</html>
当我在谷歌浏览器(更新)上加载此页面时,没有任何反应,页面保持空白。 我已请求浏览器应用程序的 API 密钥(带有引用者)并在方法 gapi.client.setApiKey 中复制。
谁能帮帮我?
谢谢
【问题讨论】:
-
控制台是否显示任何错误? (在 chrome 中按 ctrl+shift+j)
-
另外,在
request.execute(onSearchResponse);之后你有一个无与伦比的}.. -
试试答案上的例子
-
request.execute(onSearchResponse); 之后的 }关闭函数 search() {。控制台显示以下错误:无法将消息发布到文件://。收件人的来源为空
标签: javascript youtube-api youtube-javascript-api