admin管理员组

文章数量:1425732

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'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?

Share Improve this question asked Aug 6, 2014 at 12:36 kuba12kuba12 2651 gold badge4 silver badges15 bronze badges 11
  • Please post the link from the tutorial. I would like to see what you are using as an example. – snowYetis Commented Aug 6, 2014 at 12:46
  • javascriptplayground./blog/2013/06/node-and-google-oauth – kuba12 Commented Aug 6, 2014 at 12:48
  • I have never used oAuth alone. I have always used it together with the application I want to use oAuth with. So, what are you trying to use oAuth with? – snowYetis Commented Aug 6, 2014 at 12:53
  • I thought it can be without any application because I only need email, first name and last name to retrieve from Google. So I don't know. It can be calendar for example. – kuba12 Commented Aug 6, 2014 at 13:00
  • Does anyone know what's wrong? :( – kuba12 Commented Aug 6, 2014 at 13:21
 |  Show 6 more ments

2 Answers 2

Reset to default 5

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./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./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./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.

本文标签: javascriptHow to discover APIs in Google OAuth2 in NodejsStack Overflow