nodejs googleapis, authClient.request is not a function
Asked Answered
P

3

13

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.

Psychokinesis answered 21/9, 2016 at 16:2 Comment(0)
A
3

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.

Araucaria answered 4/11, 2016 at 8:48 Comment(1)
const auth = new googleAuth(); TypeError: googleAuth is not a constructorDollarfish
A
0

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

Ancon answered 25/10, 2017 at 17:0 Comment(0)
D
0

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

import { google } from 'googleapis'

const client = new google.auth.GoogleAuth({
   keyFile: 'credentials.json',
 })
Disappear answered 7/3, 2023 at 18:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.