Showing
4 changed files
with
46 additions
and
29 deletions
Youtube API/playlist.js
deleted
100644 → 0
| 1 | -var {google} =require('googleapis'); | ||
| 2 | -var service = google.youtube('v3'); | ||
| 3 | -const oauth2Service = require('./oauth2'); | ||
| 4 | - | ||
| 5 | -function getPlaylistData(oauth2Client, channelId) { | ||
| 6 | - service.playlists.list({ | ||
| 7 | - auth: oauth2Client, | ||
| 8 | - part: 'id, snippet', | ||
| 9 | - fields: 'items(id, snippet(title, description, thumbnails(default(url))))', | ||
| 10 | - channelId: channelId | ||
| 11 | - }, function(err, response) { | ||
| 12 | - if(err) { | ||
| 13 | - console.log('The API returned an error: ' + err); | ||
| 14 | - return; | ||
| 15 | - } | ||
| 16 | - var channels = response.data.items; | ||
| 17 | - if (channels.length == 0){ | ||
| 18 | - console.log('No channel found.') | ||
| 19 | - } else { | ||
| 20 | - console.log(JSON.stringify(channels, null, 4)); | ||
| 21 | - } | ||
| 22 | - }); | ||
| 23 | -} | ||
| 24 | - | ||
| 25 | -oauth2Service.refreshClient() | ||
| 26 | - .then((client) => { | ||
| 27 | - getPlaylistData(client, 'UCx6jsZ02B4K3SECUrkgPyzg'); | ||
| 28 | - }) | ||
| 29 | - .catch(console.error); | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
Youtube API/playlistbyid.js
0 → 100644
| 1 | +const { google } = require("googleapis"); | ||
| 2 | +var service = google.youtube('v3'); | ||
| 3 | +const oauth2Service = require('./oauth2'); | ||
| 4 | + | ||
| 5 | +async function getPlaylistItemData(oauth2Client, playlistId) { | ||
| 6 | + const res = await service.playlistItems.list({ | ||
| 7 | + auth: oauth2Client, | ||
| 8 | + part: 'snippet', | ||
| 9 | + fields: 'items(snippet(title))', //제목 정보만 필요함 | ||
| 10 | + maxResults: 50, | ||
| 11 | + playlistId: playlistId | ||
| 12 | + }); | ||
| 13 | + | ||
| 14 | + if (res.data.items == null || res.data.items.length === 0) { | ||
| 15 | + throw new Error("데이터가 존재하지 않습니다."); | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + return res.data; | ||
| 19 | +} | ||
| 20 | + | ||
| 21 | +oauth2Service.refreshClient() //getPlaylistItemData(client, 재생목록의 주소) 예시로 슬픈노래 재생목록을 가져옴. | ||
| 22 | + .then(client => getPlaylistItemData(client, 'PLJrlhDfEQCMD0SG8WCSbUjztMAYnVyuuY')) | ||
| 23 | + .then(data => { | ||
| 24 | + console.log(JSON.stringify(data.items, null, 4)); | ||
| 25 | + }) | ||
| 26 | + .catch(error => console.error); | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
Youtube API/구현 예정.txt
0 → 100644
Youtube API/주의 사항.txt
0 → 100644
| 1 | +oauth2.js 실행할 필요 없이 playlistbyid.js 만 실행하셔도 될 것 같습니다. | ||
| 2 | + | ||
| 3 | +실행하기 이전에, 제가 package 파일을 다루는걸 몰라서 | ||
| 4 | + | ||
| 5 | +아직은 수동으로 npm install 을 진행해주셔야 할 것 같습니다. | ||
| 6 | + | ||
| 7 | +oauth2.js 와 playlistbyid.js 에 있는 require 항목들을 모두 install 해주시고 진행해주시면 됩니다. | ||
| 8 | + | ||
| 9 | +playlistbyid.js를 실행하시면 브라우저 창에서 구글 아이디를 통해 유튜브 api를 이용할 수 있도록 인증하는 절차를 거치게 됩니다. | ||
| 10 | + | ||
| 11 | +확인되지 않은 앱이라고 뜨시면 고급으로 이동하여 안전하지 않은 페이지로 이동하시면 됩니다. | ||
| 12 | + | ||
| 13 | +모든 인증이 끝나면 Completed 라는 메시지가 뜹니다. | ||
| 14 | + | ||
| 15 | +재생목록의 주소를 입력하면 재생목록의 영상 제목을 출력하도록 하였습니다. | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment