google-auth-library-nodejs: Update docs: GoogleAuth is not a constructor

Hi I am using node.js the code documentation: https://developers.google.com/identity/sign-in/web/backend-auth

var GoogleAuth = require('google-auth-library');
var auth = new GoogleAuth;

as Import for this lib

but I get this error

TypeError: GoogleAuth is not a constructor

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 16
  • Comments: 32 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Hi I fix this I just remove the version I install before, that was v1 and install the v 0.12.0 now is working.

  1. npm uninstall google-auth-library --save
  2. npm install google-auth-library@0.12.0 --save

now is work for me.

@finlay-harrison, I solved my issue by changing:

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

which was given in an example from the documentation, to this:

const { OAuth2Client } = require('google-auth-library');
...
var oauth2Client = new OAuth2Client(clientId, clientSecret, redirectUrl);

as @sfreeman422 already mentioned.

I made authentication work in 1.0.0 this way:

const { OAuth2Client } = require('google-auth-library');
const idToken = ...; // the token received from the JS client
const audiance = "...apps.googleusercontent.com"; // gapi client id
var gapiClient = new OAuth2Client(audiance, '', '');
gapiClient.verifyIdToken({idToken, audiance}).then(login => {
    var payload = login.getPayload();
    var userId = payload.email;
    ...

Yah I had heard older versions worked, however moving forward it’s not the ideal solution. Google needs to update these reference docs! Frustrating to learn a product that is several versions with major changes ahead of all the reference sheets new users are relying on.

For anyone coming here from the Google Sheets API getting started and running into the issues with the auth constructor, this works for me:

  • First performed an uninstall of google-auth-library, then reinstalled without the version specification npm install --save google-auth-library

Then use the following imports from google:

const google = require("googleapis"); const { OAuth2Client } = require("google-auth-library");

Then in the authorize function, update to:

const auth = new OAuth2Client(clientId, clientSecret, redirectUrl);

instead of the previous googleAuth.

Then update necessary areas with the new auth const

Worked on this a bit more last night and rather than importing via:

const { GoogleAuth } = require('google-auth-library');

I was able to get the example to semi-work by importing OAuth2Client directly via:

const { OAuth2Client } = require('google-auth-library');

I then removed the line that created a new instance of googleAuth and replaced the auth.OAuth2Client line with OAuth2Client(parameters) where parameters are whatever the documentation calls for (memory is a little hazy right now and I am not near my development rig).

Mind you, this will only half work. It appears the docs are out of date as when I then tested the quickstart.js, I ended up getting to the part where you retrieve your token and enter it via the CLI. After entering, the program continues and makes its request but it appears that invalid parameters are being passed to Axios somewhere inside of /lib/apirequest.ts. I am still digging through the code to try and determine where this is going wrong.

Is that to say that you are using an older version than the most current from Google? That does not seem like a fix for this issue in my opinion.

The documentation in the googleapis node package includes information about authentication. It worked for me. They do it like this:

var {google} = require('googleapis');
var OAuth2 = google.auth.OAuth2;
 
var oauth2Client = new OAuth2(YOUR_CLIENT_ID, YOUR_CLIENT_SECRET,  YOUR_REDIRECT_URL);

So you don’t need to install the google-auth-library package

sorry for that horrible code dump. I followed this tutorial: http://voidcanvas.com/node-js-googleapis-v4-spreadsheet/

Yep! That is precisely the error I was getting. If you dig a bit deeper, it looks like Axios instead expects this field to be called data and that, in order to specify json, it should be listed as data: 'json'. Once you make that change, your request will go through, but you will get a ‘malformed request’ error message when trying to use the YouTube Data API (that is what I was testing with). I plan to dig in a little deeper tonight, and if I can find a resolution, I will be making a pull request as this essentially breaks the YouTube Data API functionality, which my app will rely on.