2017-02-13 110 views
-1

enter image description here如何讓YouTube播放的視頻ID與視頻標題

我需要的YouTube播放視頻ID和視頻標題中使用API​​密鑰,只要我們需要在一個YouTube播放列表過去五年更新視頻如何檢索。如何得到它請給我一個完美的鏈接,以便我實現這一目標。 我們在下面

enter code here 

<script> 
 
var channelName = 'mipaltan'; 
 
var vidHeight = 350; 
 
var vidWidth = 650; 
 
var vidMaxResult =7; // Maximum can be 50 
 

 
$(document).ready(function() { 
 
$.get("https://www.googleapis.com/youtube/v3/channels", { 
 
part: 'contentDetails', 
 
forUsername: channelName, 
 
key: 'AIzaSyCT8kXaxJ2l29vYg4HBdYy36H-PhAH-Teg' //Browser API Key 
 
}, 
 
function (data) { 
 
$.each(data.items, function (i, item) { 
 
console.log(item); // See in Browser Console 
 
pid = item.contentDetails.relatedPlaylists.uploads; 
 
getVideos(pid); 
 
}) 
 
} 
 
); 
 
function getVideos(pid) { 
 
$.get("https://www.googleapis.com/youtube/v3/playlistItems", 
 
{ 
 
part: 'snippet', 
 
maxResults: vidMaxResult, 
 
playlistId: pid, 
 
key: 'AIzaSyCT8kXaxJ2l29vYg4HBdYy36H-PhAH-Teg' //Browser API Key 
 
}, 
 
function (data) { 
 
var outputVideo; 
 
$.each(data.items, function (i, item) { 
 
console.log(item); // See in Browser Console 
 
vidId = item.snippet.resourceId.videoId; 
 
thumbnails = item.snippet.thumbnails.default.url; 
 
texturl = 'https://www.youtube.com/embed/' + vidId; 
 
mainurl = "'" + texturl + "'"; 
 
outputVideo = '<div style="float:left"><img style="width: 93px;height:65px; border-radius: 5px;margin-right:1px;" src="' + thumbnails + '"onclick="newSrc(' + mainurl + ')" /></div>'; 
 

 
$('#result').append(outputVideo); 
 
}) 
 
} 
 

 
); 
 
} 
 
}); 
 

 
</script> 
 
<script> 
 
function newSrc(testurl) { 
 
document.getElementById("MyFrame").src = testurl; 
 
} 
 
</script> 
 
</head> 
 
<body> 
 
<div id="container"> 
 
<iframe id="MyFrame"; width="670"; height="350"; src="https://www.youtube.com/embed/Yx9M-6cx8wA" frameborder="0" allowfullscreen></iframe> 
 
<div style="padding-left:5px", id="result" class ="footer-widget"> 
 
</div> 
 
</div> 
 
</body>

+3

你嘗試過什麼?你的代碼到目前爲止看起來如何?你有沒有收到任何錯誤信息?請給點配合。 StackOverflow不是讓人們爲你寫代碼。 –

回答

0

我複製你的代碼,並添加jQuery代碼。控制檯顯示你已經達到了你的目標?

enter image description here

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<script> 
 
var channelName = 'mipaltan'; 
 
var vidHeight = 350; 
 
var vidWidth = 650; 
 
var vidMaxResult =7; // Maximum can be 50 
 

 
$(document).ready(function() { 
 
$.get("https://www.googleapis.com/youtube/v3/channels", { 
 
part: 'contentDetails', 
 
forUsername: channelName, 
 
key: 'AIzaSyCT8kXaxJ2l29vYg4HBdYy36H-PhAH-Teg' //Browser API Key 
 
}, 
 
function (data) { 
 
$.each(data.items, function (i, item) { 
 
console.log(item); // See in Browser Console 
 
pid = item.contentDetails.relatedPlaylists.uploads; 
 
getVideos(pid); 
 
}) 
 
} 
 
); 
 
function getVideos(pid) { 
 
$.get("https://www.googleapis.com/youtube/v3/playlistItems", 
 
{ 
 
part: 'snippet', 
 
maxResults: vidMaxResult, 
 
playlistId: pid, 
 
key: 'AIzaSyCT8kXaxJ2l29vYg4HBdYy36H-PhAH-Teg' //Browser API Key 
 
}, 
 
function (data) { 
 
var outputVideo; 
 
$.each(data.items, function (i, item) { 
 
console.log(item); // See in Browser Console 
 
vidId = item.snippet.resourceId.videoId; 
 
thumbnails = item.snippet.thumbnails.default.url; 
 
texturl = 'https://www.youtube.com/embed/' + vidId; 
 
mainurl = "'" + texturl + "'"; 
 
outputVideo = '<div style="float:left"><img style="width: 93px;height:65px; border-radius: 5px;margin-right:1px;" src="' + thumbnails + '"onclick="newSrc(' + mainurl + ')" /></div>'; 
 

 
$('#result').append(outputVideo); 
 
}) 
 
} 
 

 
); 
 
} 
 
}); 
 

 
</script> 
 
<script> 
 
function newSrc(testurl) { 
 
document.getElementById("MyFrame").src = testurl; 
 
} 
 
</script> 
 
</head> 
 
<body> 
 
<div id="container"> 
 
<iframe id="MyFrame"; width="670"; height="350"; src="https://www.youtube.com/embed/Yx9M-6cx8wA" frameborder="0" allowfullscreen></iframe> 
 
<div style="padding-left:5px", id="result" class ="footer-widget"> 
 
</div> 
 
</div> 
 
</body>

+0

謝謝Chris Chen,我非常感謝你的工作。 –