Last active
September 30, 2016 04:11
-
-
Save jirolabo/7a1c1fcbc1c1cd3aa99cc5039a58845e to your computer and use it in GitHub Desktop.
YouTube Data API (v3) からデータ取得($.ajax)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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