Skip to content

Commit 32f087d

Browse files
Update nodejs quickstart example
- update code sample to address breaking changes in google-api-nodejs-client - fs asynchronous functions display an error without a callback
1 parent 3732e66 commit 32f087d

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

javascript/nodejs-quickstart.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var fs = require('fs');
22
var readline = require('readline');
3-
var google = require('googleapis');
4-
var googleAuth = require('google-auth-library');
3+
var {google} = require('googleapis');
4+
var OAuth2 = google.auth.OAuth2;
55

66
// If modifying these scopes, delete your previously saved credentials
77
// at ~/.credentials/youtube-nodejs-quickstart.json
@@ -31,8 +31,7 @@ function authorize(credentials, callback) {
3131
var clientSecret = credentials.installed.client_secret;
3232
var clientId = credentials.installed.client_id;
3333
var redirectUrl = credentials.installed.redirect_uris[0];
34-
var auth = new googleAuth();
35-
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
34+
var oauth2Client = new OAuth2(clientId, clientSecret, redirectUrl);
3635

3736
// Check if we have previously stored a token.
3837
fs.readFile(TOKEN_PATH, function(err, token) {
@@ -90,7 +89,10 @@ function storeToken(token) {
9089
throw err;
9190
}
9291
}
93-
fs.writeFile(TOKEN_PATH, JSON.stringify(token));
92+
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
93+
if (err) throw err;
94+
console.log('Token stored to ' + TOKEN_PATH);
95+
});
9496
console.log('Token stored to ' + TOKEN_PATH);
9597
}
9698

@@ -110,7 +112,7 @@ function getChannel(auth) {
110112
console.log('The API returned an error: ' + err);
111113
return;
112114
}
113-
var channels = response.items;
115+
var channels = response.data.items;
114116
if (channels.length == 0) {
115117
console.log('No channel found.');
116118
} else {

0 commit comments

Comments
 (0)