Skip to content

Instantly share code, notes, and snippets.

@jirolabo
Last active September 30, 2016 04:11
Show Gist options
  • Save jirolabo/7a1c1fcbc1c1cd3aa99cc5039a58845e to your computer and use it in GitHub Desktop.
Save jirolabo/7a1c1fcbc1c1cd3aa99cc5039a58845e to your computer and use it in GitHub Desktop.
YouTube Data API (v3) からデータ取得($.ajax)
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
var API_KEY = '__YOUR_API_KEY__';
function requestUserUploadsPlaylistId() {
$.ajax({
url: 'https://www.googleapis.com/youtube/v3/channels',
type: 'GET',
data: {
key: API_KEY,
part: 'contentDetails',
forUsername: 'GoogleDevelopers'
}
}).done(function (data) {
var playlistId = data.items[0].contentDetails.relatedPlaylists.uploads;
requestVideoPlaylist(playlistId)
});
}
function requestVideoPlaylist(playlistId) {
$.ajax({
url: 'https://www.googleapis.com/youtube/v3/playlistItems',
type: 'GET',
data: {
key: API_KEY,
part: 'snippet',
playlistId: playlistId,
maxResults: 10
}
}).done(function (data) {
var playlistItems = data.items;
if (playlistItems) {
$.each(playlistItems, function (index, item) {
displayResult(item.snippet);
});
} else {
$('#video-container').html('Sorry you have no uploaded videos');
}
});
}
function displayResult(videoSnippet) {
var title = videoSnippet.title;
var videoId = videoSnippet.resourceId.videoId;
$('body').append('<p>' + title + ' - ' + videoId + '</p>');
}
requestUserUploadsPlaylistId();
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment