Skip to content

Instantly share code, notes, and snippets.

@jirolabo
Last active September 30, 2016 04:02
Show Gist options
  • Save jirolabo/eb877e834bb48204c5b8c918d36e51bf to your computer and use it in GitHub Desktop.
Save jirolabo/eb877e834bb48204c5b8c918d36e51bf to your computer and use it in GitHub Desktop.
YouTube Data API (v3) からデータ取得(Google APIライブラリ )
<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() {
var request = gapi.client.youtube.channels.list({
part: 'contentDetails',
forUsername: 'GoogleDevelopers'
});
request.execute(function (response) {
var playlistId = response.result.items[0].contentDetails.relatedPlaylists.uploads;
requestVideoPlaylist(playlistId)
});
}
function requestVideoPlaylist(playlistId) {
var requestOptions = {
playlistId: playlistId,
part: 'snippet',
maxResults: 10
};
var request = gapi.client.youtube.playlistItems.list(requestOptions);
request.execute(function (response) {
var playlistItems = response.result.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>');
}
function googleApiClientReady() {
gapi.client.setApiKey(API_KEY);
gapi.client.load('youtube', 'v3', function () {
requestUserUploadsPlaylistId();
});
}
</script>
<script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment