|
| 1 | +var fs = require('fs'); |
| 2 | +var readline = require('readline'); |
| 3 | +var google = require('googleapis'); |
| 4 | +var googleAuth = require('google-auth-library'); |
| 5 | + |
| 6 | +// If modifying these scopes, delete your previously saved credentials |
| 7 | +// at ~/.credentials/youtube-nodejs-quickstart.json |
| 8 | +var SCOPES = ['https://www.googleapis.com/auth/youtube.readonly']; |
| 9 | +var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || |
| 10 | + process.env.USERPROFILE) + '/.credentials/'; |
| 11 | +var TOKEN_PATH = TOKEN_DIR + 'youtube-nodejs-quickstart.json'; |
| 12 | + |
| 13 | +// Load client secrets from a local file. |
| 14 | +fs.readFile('client_secret.json', function processClientSecrets(err, content) { |
| 15 | + if (err) { |
| 16 | + console.log('Error loading client secret file: ' + err); |
| 17 | + return; |
| 18 | + } |
| 19 | + // Authorize a client with the loaded credentials, then call the YouTube API. |
| 20 | + authorize(JSON.parse(content), getChannel); |
| 21 | +}); |
| 22 | + |
| 23 | +/** |
| 24 | + * Create an OAuth2 client with the given credentials, and then execute the |
| 25 | + * given callback function. |
| 26 | + * |
| 27 | + * @param {Object} credentials The authorization client credentials. |
| 28 | + * @param {function} callback The callback to call with the authorized client. |
| 29 | + */ |
| 30 | +function authorize(credentials, callback) { |
| 31 | + var clientSecret = credentials.installed.client_secret; |
| 32 | + var clientId = credentials.installed.client_id; |
| 33 | + var redirectUrl = credentials.installed.redirect_uris[0]; |
| 34 | + var auth = new googleAuth(); |
| 35 | + var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl); |
| 36 | + |
| 37 | + // Check if we have previously stored a token. |
| 38 | + fs.readFile(TOKEN_PATH, function(err, token) { |
| 39 | + if (err) { |
| 40 | + getNewToken(oauth2Client, callback); |
| 41 | + } else { |
| 42 | + oauth2Client.credentials = JSON.parse(token); |
| 43 | + callback(oauth2Client); |
| 44 | + } |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Get and store new token after prompting for user authorization, and then |
| 50 | + * execute the given callback with the authorized OAuth2 client. |
| 51 | + * |
| 52 | + * @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for. |
| 53 | + * @param {getEventsCallback} callback The callback to call with the authorized |
| 54 | + * client. |
| 55 | + */ |
| 56 | +function getNewToken(oauth2Client, callback) { |
| 57 | + var authUrl = oauth2Client.generateAuthUrl({ |
| 58 | + access_type: 'offline', |
| 59 | + scope: SCOPES |
| 60 | + }); |
| 61 | + console.log('Authorize this app by visiting this url: ', authUrl); |
| 62 | + var rl = readline.createInterface({ |
| 63 | + input: process.stdin, |
| 64 | + output: process.stdout |
| 65 | + }); |
| 66 | + rl.question('Enter the code from that page here: ', function(code) { |
| 67 | + rl.close(); |
| 68 | + oauth2Client.getToken(code, function(err, token) { |
| 69 | + if (err) { |
| 70 | + console.log('Error while trying to retrieve access token', err); |
| 71 | + return; |
| 72 | + } |
| 73 | + oauth2Client.credentials = token; |
| 74 | + storeToken(token); |
| 75 | + callback(oauth2Client); |
| 76 | + }); |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Store token to disk be used in later program executions. |
| 82 | + * |
| 83 | + * @param {Object} token The token to store to disk. |
| 84 | + */ |
| 85 | +function storeToken(token) { |
| 86 | + try { |
| 87 | + fs.mkdirSync(TOKEN_DIR); |
| 88 | + } catch (err) { |
| 89 | + if (err.code != 'EEXIST') { |
| 90 | + throw err; |
| 91 | + } |
| 92 | + } |
| 93 | + fs.writeFile(TOKEN_PATH, JSON.stringify(token)); |
| 94 | + console.log('Token stored to ' + TOKEN_PATH); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Lists the names and IDs of up to 10 files. |
| 99 | + * |
| 100 | + * @param {google.auth.OAuth2} auth An authorized OAuth2 client. |
| 101 | + */ |
| 102 | +function getChannel(auth) { |
| 103 | + var service = google.youtube('v3'); |
| 104 | + service.channels.list({ |
| 105 | + auth: auth, |
| 106 | + part: 'snippet,contentDetails,statistics', |
| 107 | + forUsername: 'GoogleDevelopers' |
| 108 | + }, function(err, response) { |
| 109 | + if (err) { |
| 110 | + console.log('The API returned an error: ' + err); |
| 111 | + return; |
| 112 | + } |
| 113 | + var channels = response.items; |
| 114 | + if (channels.length == 0) { |
| 115 | + console.log('No channel found.'); |
| 116 | + } else { |
| 117 | + console.log('This channel\'s ID is %s. Its title is \'%s\', and ' + |
| 118 | + 'it has %s views.', |
| 119 | + channels[0].id, |
| 120 | + channels[0].snippet.title, |
| 121 | + channels[0].statistics.viewCount); |
| 122 | + } |
| 123 | + }); |
| 124 | +} |
0 commit comments