YouTube Videos
YouTube Videos
// Load the YouTube API client library
gapi.load('client', init);
// Initialize the API client library
function init() {
gapi.client.init({
apiKey: 'YOUR_API_KEY',
discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest']
}).then(function() {
// Call the YouTube API to retrieve information about your channel
gapi.client.youtube.channels.list({
part: 'contentDetails',
mine: true
}).then(function(response) {
var playlistId = response.result.items[0].contentDetails.relatedPlaylists.uploads;
// Call the YouTube API to retrieve information about your videos
gapi.client.youtube.playlistItems.list({
part: 'snippet',
playlistId: playlistId,
maxResults: 50
}).then(function(response) {
var videoList = document.getElementById('videoList');
response.result.items.forEach(function(item) {
var title = item.snippet.title;
var videoId = item.snippet.resourceId.videoId;
var thumbnailUrl = item.snippet.thumbnails.medium.url;
var li = document.createElement('li');
var a = document.createElement('a');
var img = document.createElement('img');
img.src = thumbnailUrl;
a.href = 'https://www.youtube.com/watch?v=' + videoId;
a.textContent = title;
a.target = '_blank';
a.rel = 'noopener noreferrer';
a.appendChild(img);
li.appendChild(a);
videoList.appendChild(li);
});
});
});
});
}
Comments
Post a Comment