Showing
2 changed files
with
103 additions
and
0 deletions
Youtube API/oauth2.js
0 → 100644
| 1 | +const { google } = require("googleapis"); | ||
| 2 | +var http = require('http'); | ||
| 3 | +var opn = require('opn'); | ||
| 4 | +var destroyer = require('destroyer'); | ||
| 5 | +var url = require('url'); | ||
| 6 | +var enableDestroy = require('server-destroy'); | ||
| 7 | +const oauth2Client = new google.auth.OAuth2( | ||
| 8 | + "394965702516-q817fn2gj8r8sqoudhp29k1rd71ku42j.apps.googleusercontent.com", | ||
| 9 | + "xgo5tcTwsmg2alNumeY1Drj_", | ||
| 10 | + "http://localhost:3031/api/oauth2callback" | ||
| 11 | +); | ||
| 12 | + | ||
| 13 | +const scopes = [ | ||
| 14 | + 'https://www.googleapis.com/auth/youtube', | ||
| 15 | + 'https://www.googleapis.com/auth/youtube.force-ssl', | ||
| 16 | + 'https://www.googleapis.com/auth/youtube.readonly', | ||
| 17 | + 'https://www.googleapis.com/auth/youtubepartner' | ||
| 18 | +]; | ||
| 19 | + | ||
| 20 | +let refreshToken = ''; | ||
| 21 | + | ||
| 22 | +async function authenticate() { | ||
| 23 | + | ||
| 24 | + return new Promise((resolve, reject) => { | ||
| 25 | + | ||
| 26 | + if (refreshToken !== "") { | ||
| 27 | + oauth2Client.setCredentials({ | ||
| 28 | + refresh_token: refreshToken | ||
| 29 | + }); | ||
| 30 | + | ||
| 31 | + oauth2Client.getAccessToken((err, ttoken) => { | ||
| 32 | + if (err) { | ||
| 33 | + reject(e); | ||
| 34 | + } | ||
| 35 | + resolve(oauth2Client); | ||
| 36 | + }); | ||
| 37 | + } else { | ||
| 38 | + const authorizeUrl = oauth2Client.generateAuthUrl({ | ||
| 39 | + access_type: 'offline', | ||
| 40 | + scope: scopes.join(' '), | ||
| 41 | + }); | ||
| 42 | + const server = http.createServer(async (req, res) => { | ||
| 43 | + try { | ||
| 44 | + if (req.url.indexOf('/api/oauth2callback') > -1) { | ||
| 45 | + const qs = new url.URL(req.url, 'http://localhost:3031').searchParams; | ||
| 46 | + res.end('Completed'); | ||
| 47 | + enableDestroy(server); | ||
| 48 | + server.destroy(); | ||
| 49 | + | ||
| 50 | + const { | ||
| 51 | + tokens | ||
| 52 | + } = await oauth2Client.getToken(qs.get('code')); | ||
| 53 | + | ||
| 54 | + refreshToken = tokens.refresh_token; | ||
| 55 | + oauth2Client.credentials = tokens; | ||
| 56 | + | ||
| 57 | + resolve(oauth2Client); | ||
| 58 | + } | ||
| 59 | + } catch (e) { | ||
| 60 | + reject(e); | ||
| 61 | + } | ||
| 62 | + }).listen(3031, () => { | ||
| 63 | + | ||
| 64 | + opn(authorizeUrl, { | ||
| 65 | + wait: false | ||
| 66 | + }).then(cp => cp.unref()); | ||
| 67 | + }); | ||
| 68 | + | ||
| 69 | + destroyer(server); | ||
| 70 | + } | ||
| 71 | + }); | ||
| 72 | +} | ||
| 73 | + | ||
| 74 | +module.exports.refreshClient=authenticate; | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
Youtube API/playlist.js
0 → 100644
| 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 |
-
Please register or login to post a comment