使用 youtube api 使用 jQuery 获取 youtube 视频的标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2478391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get youtube video's title with jQuery using youtube api
提问by AlexC
What is the easiest way to get the title from the youtybe video , for example this video title :
从youtybe视频中获取标题的最简单方法是什么,例如这个视频标题:
http://www.youtube.com/watch?v=Wp7B81Kx66o
http://www.youtube.com/watch?v=Wp7B81Kx66o
Thanks !
谢谢 !
采纳答案by easement
Use jQuery's JSONcall to the YouTube API to get the results back and then use jQuery to put the results where you want them. You can use firebug's NET tab to make sure you requests/respoonses are coming back correctly and then use console.log() to make sure you parsed the response correctly.
使用jQuery对 YouTube API的 JSON调用来获取结果,然后使用 jQuery 将结果放在您想要的位置。您可以使用 firebug 的 NET 选项卡来确保您的请求/响应正确返回,然后使用 console.log() 确保您正确解析了响应。
eg. URL:
例如。网址:
GET https://gdata.youtube.com/feeds/api/videos/(the-video-id)?v=2&alt=json
获取https://gdata.youtube.com/feeds/api/videos/(the-video-id)?v=2&alt=json
More info:
更多信息:
YouTube API for a specific video
回答by Sorry-Im-a-N00b
This is a overhauled implementation of the original answer provided by @easement using the current v3 YouTube Data API.
这是@easement 使用当前v3 YouTube Data API提供的原始答案的彻底改进实现。
In order to make a request to the API, you can use jQuery's getJSON() callto request the title from YouTube via AJAX. YouTube's v3 Data API provides 3 endpoints that can be used to get the title:
为了向 API 发出请求,您可以使用jQuery 的 getJSON() 调用通过 AJAX 从 YouTube 请求标题。YouTube 的 v3 数据 API 提供了 3 个可用于获取标题的端点:
- Snippet Title- The video's title. The property value has a maximum length of 100 characters and may contain all valid UTF-8 characters except < and >.
- Snippet Localized Title- The localized video title, again with the maximum length described above
- Full Localized Title- The full length localized video title.
- 片段标题- 视频的标题。属性值的最大长度为 100 个字符,并且可以包含除 < 和 > 之外的所有有效 UTF-8 字符。
- 片段本地化标题- 本地化视频标题,同样具有上述最大长度
- 完整本地化标题- 全长本地化视频标题。
Sample Implementation using Snippet Title
使用代码段标题的示例实现
var yt_api_key = {your YouTube api key},
yt_video_id = {your YouTube video id},
yt_snippet_endpoint = "https://www.googleapis.com/youtube/v3/videos?part=snippet&id=" + yt_video_id + "&key=" + yt_api_key;
var jqxhr = $.getJSON(yt_snippet_endpoint)
.done(function(data) {
console.log("second success callback");
var title = getTitle(data);
// do something with title here
})
.fail(function() {
console.log("error, see network tab for response details");
});
function getTitle(snippet_json_data){
var title = snippet_json_data.title;
return title;
}
Debugging tip:You can use developer tools to view Network requests (i.e Chrome's developer toolsor Firefox's Firebug) to make sure you requests/responses are coming back correctly and then use console.log() to log the returned data to make sure you parsed the response correctly.
调试提示:您可以使用开发者工具查看网络请求(即Chrome 的开发者工具或Firefox 的 Firebug)以确保您的请求/响应正确返回,然后使用 console.log() 记录返回的数据以确保您解析响应正确。
Additional Reading:YouTube Data API "getting started"
补充阅读:YouTube 数据 API“入门”
回答by Ali Habibzadeh
Try this:
尝试这个:
<script type="text/javascript">
function showMyVideos(data) {
var feed = data.feed;
var entries = feed.entry || [];
var html = ['<ul>'];
for (var i = 0; i < entries.length; i++) {
var entry = entries[i];
var title = entry.title.$t;
html.push('<li>', title, '</li>');
}
html.push('</ul>');
document.getElementById('videos').innerHTML = html.join('');
}
</script>
and this in your body part:
这在你的身体部位:
<body>
<div id="videos">
<script
type="text/javascript"
src="http://gdata.youtube.com/feeds/users/GoogleDevelopers/uploads?alt=json-in-script&format=5&callback=showMyVideos">
</script>
</div>
</body>
回答by Supot Sawangpiriyakij
Try this for show Youtube feature picture in front of Jquery list view.
试试这个,在 Jquery 列表视图前显示 Youtube 功能图片。
<script>
function showMyVideos(data) {
var feed = data.feed;
var entries = feed.entry || [];
var html = ['<ul data-role="listview">'];
for (var i = 0; i < entries.length; i++) {
var entry = entries[i];
var title = entry.title.$t;
var feature = entry.content.$t.substring(entry.content.$t.indexOf("src=")+5);
feature = feature.substring(0,feature.indexOf('"'));
html.push('<li><img src="' ,feature , '" />' , title , '<p>' , feature, '</p></li>');
}
html.push('</ul>');
document.getElementById('videos').innerHTML = html.join('');
}
</script>
<div id="videos">
<script
src="http://gdata.youtube.com/feeds/users/mostviewcomedy/uploads?alt=json-in-script&format=5&callback=showMyVideos">
</script>
</div>