Last active
September 30, 2016 04:02
-
-
Save jirolabo/eb877e834bb48204c5b8c918d36e51bf to your computer and use it in GitHub Desktop.
YouTube Data API (v3) からデータ取得(Google APIライブラリ )
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() { | |
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