How to discover APIs in Google OAuth2 in Node.js

I've got a problem with .discover method in Node.js. I've got such a piece of code:

googleapis.discover('oauth2', 'v2').execute(function(err, client){
    if(!err)
        callback(client);
});

And it throws error: TypeError: Object # has no method 'discover' But in all tutorials there is such a method mentioned. Does anyone know what's wrong?

I got the same error. I think they updated the googleapis client for Node.

Try follow the new syntax or use older version:

var google = require('googleapis');
var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
var drive = google.drive({ version: 'v2', auth: oauth2Client });

I got the same error. But after looking around for a few hours. I came across this question and realized they might have changed the API and haven't updated the official documentation.

I was also trying this piece of code from this link (https://developers.google.com/drive/web/quickstart/quickstart-nodejs)

var googleapis = require('googleapis'),
    readline = require('readline');

var CLIENT_ID = 'YOUR CLIENT ID HERE',
    CLIENT_SECRET = 'YOUR CLIENT SECRET HERE',
    REDIRECT_URL = 'YOUR REDIRECT URL HERE',
    SCOPE = 'https://www.googleapis.com/auth/drive.file';

var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

var auth = new googleapis.OAuth2Client(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);

googleapis.discover('drive', 'v2').execute(function(err, client) {
  var url = auth.generateAuthUrl({ scope: SCOPE });
  var getAccessToken = function(code) {
    auth.getToken(code, function(err, tokens) {
      if (err) {
        console.log('Error while trying to retrieve access token', err);
        return;
      }
      auth.credentials = tokens;
      upload();
    });
  };
  var upload = function() {
    client.drive.files
      .insert({ title: 'My Document', mimeType: 'text/plain' })
      .withMedia('text/plain', 'Hello World!')
      .withAuthClient(auth).execute(console.log);
  };
  console.log('Visit the url: ', url);
  rl.question('Enter the code here:', getAccessToken);
});

After trying for hours and hours. I changed my quickstart.js to this after looking at some other docs/tutorials on the internet.

var googleapis = require('googleapis');
var OAuth2 = googleapis.auth.OAuth2;
var readline = require('readline');

var CLIENT_ID = 'YOUR CLIENT ID HERE',
    CLIENT_SECRET = 'YOUR CLIENT SECRET HERE',
    REDIRECT_URL = 'YOUR REDIRECT URL HERE',
    SCOPE = 'https://www.googleapis.com/auth/drive.file';

var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

var oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
var drive = googleapis.drive({ version: 'v2', auth: oauth2Client });

var execute = function(err, client) {  
  var url = oauth2Client.generateAuthUrl({ scope: SCOPE });
  var getAccessToken = function(code) {
    oauth2Client.getToken(code, function(err, tokens) {
      if (err) {
        console.log('Error while trying to retrieve access token', err);
        return;
      }
      oauth2Client.credentials = tokens;
      upload();
    });
  };
  var upload = function() {
    console.log(client)
    client.drive.files
      .insert({ title: 'My Document', mimeType: 'text/plain' })
      .withMedia('text/plain', 'Hello World!')
      .withAuthClient(oauth2Client).execute(console.log);
  };
  console.log('Visit the url: ', url);
  rl.question('Enter the code here:', getAccessToken);
};

execute();

It started working but although I got it to output the URL to go to, I get an error after pasting the authorization code from the URL. The Client object in the upload function is undefined so I get an type error saying "cannot read property 'drive' of undefined". If someone knows the problem, please do let me know. I am also just learning NodeJS.