EthanLWillis
EthanLWillis

Reputation: 950

nodejs googleapis, authClient.request is not a function

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.

Upvotes: 13

Views: 11592

Answers (3)

TeemuK
TeemuK

Reputation: 2529

Instead of OAuth, you have to use GoogleAuth with service account:

import { google } from 'googleapis'

const client = new google.auth.GoogleAuth({
   keyFile: 'credentials.json',
 })

Upvotes: 0

Stretch0
Stretch0

Reputation: 9241

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

Upvotes: 0

sandeep rajbhandari
sandeep rajbhandari

Reputation: 1296

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.

Upvotes: 3

Related Questions