nodejs googleapis, authClient.request is not a fun

2019-04-23 18:22发布

问题:

I am creating an oauth2client in one function like so and returning it. I actually do pass in the clien id, secret, redirect url, and credentials. Those are all correct from what I have checked.

var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
...
credentials = {
      access_token: accessToken,
      refresh_token: refreshToken
};
oauth2Client.setCredentials(credentials);

I then do this in the function where the oauth2client object is returned:

var plus = google.plus('v1');
console.log(JSON.stringify(oauth_client));
plus.people.get({ userId: 'me' , auth: oauth_client}, function(err, response) {
    if(err) {
        console.log(err);
    } else {
        console.log(JSON.stringify(response));
        return response;
    }
});

However I then get an error message saying that authClient.request is not a function.

TypeError: authClient.request is not a function at createAPIRequest (/node_modules/googleapis/lib/apirequest.js:180:22)

I'm not sure why I get this error. I also did console.log(JSON.stringify(oauth_client)) to check for a request function and I didn't see any. Someone mentioned that this can't display the full prototype chain and that the request function might actually be there.

回答1:

The problem is with "oauth_client".I used "google-auth-library" to authenticate.

var googleAuth = require('google-auth-library');
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
oauth2Client.credentials = credentials;

and then use this oauth2Client as oauth_client.



回答2:

Not sure if you ever resolved this but try checking the scopes you have permissions for.

I was getting this error and turns out I had my scope set to 'https://www.googleapis.com/auth/youtube.readonly' and then when I changed my scope to 'https://www.googleapis.com/auth/youtube.upload' & 'https://www.googleapis.com/auth/youtube' I was able to upload videos instead of getting the error authClient.request is not a function



回答3:

I had the same problem, but did not have enough experience with OAuth2 framework.

I manage to work my way around id by removing node modules already installed on the current directory and running:

npm install googleapis@27 --save

I was having this trouble while trying to follow the google drive API tutorial at https://developers.google.com/drive/api/v3/quickstart/nodejs