Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs] TypeError: Cannot read property 'OAuth2' of undefined #355

Closed
celestebancos opened this issue Apr 29, 2018 · 5 comments

Comments

Projects
None yet
3 participants
@celestebancos
Copy link

commented Apr 29, 2018

I am following this tutorial: https://developers.google.com/sheets/api/quickstart/nodejs

I copied and pasted the sample code with no changes but am getting this error when I run it:

const OAuth2Client = google.auth.OAuth2();
^

TypeError: Cannot read property 'OAuth2' of undefined
at Object. (/Users/pebbles/Documents/GitHub/KnockoutTutorial/quickstart.js:4:34)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3

I am very new and have no idea what I'm doing; I'm just doing my best to follow instructions and learn as I go. I'm filing this issue based on @JustinBeckwith's comments here because I think I'm having the same problem as AkshayaSaravanann and don't see that any new issue was filed: #251

@celestebancos

This comment has been minimized.

Copy link
Author

commented Apr 29, 2018

Instead of this line:

const OAuth2Client = google.auth.OAuth2();

I tried using the following:

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

(I got this from nagyzsolthun's comment on Issue #251)

It successfully gave me the url to get the code to enter. When I entered the code, I got these warnings:

(node:2477) UnhandledPromiseRejectionWarning: TypeError: google.sheets is not a function
at listMajors (/Users/pebbles/Documents/GitHub/KnockoutTutorial/quickstart.js:90:25)
at oAuth2Client.getToken (/Users/pebbles/Documents/GitHub/KnockoutTutorial/quickstart.js:72:23)
at /Users/pebbles/Documents/GitHub/KnockoutTutorial/node_modules/google-auth-library/build/src/auth/oauth2client.js:148:46
at
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:2477) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:2477) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

And when I tried rerunning the quickstart.js, I got this:

/Users/pebbles/Documents/GitHub/KnockoutTutorial/quickstart.js:90
const sheets = google.sheets({version: 'v4', auth});
^

TypeError: google.sheets is not a function
at listMajors (/Users/pebbles/Documents/GitHub/KnockoutTutorial/quickstart.js:90:25)
at fs.readFile (/Users/pebbles/Documents/GitHub/KnockoutTutorial/quickstart.js:49:5)
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)

@celestebancos

This comment has been minimized.

Copy link
Author

commented Apr 29, 2018

I added curly brackets and now it works!

const { google } = require('googleapis');

instead of

const google = require('googleapis');

I got the idea from this thread:
https://stackoverflow.com/questions/48816324/gmail-api-quickstart-failing-at-google-gmailv1

Now to research what those brackets did and why it fixed things!

@JustinBeckwith

This comment has been minimized.

Copy link
Contributor

commented Apr 29, 2018

Greetings! And apologies for the trouble. That doc looks to still be out of date. In v25, we made a breaking change that switched the syntax of the import statement from this:

const google = require('googleapis');

To this....

const {google} = require('googleapis');

This is what's called a destructuring assignment. It's functionally equivalent to saying:

const google = require('googleapis').google;

We made this change as we moved towards es module style syntax, which made it A LOT easier for us to support TypeScript.

Again - I apologize that our doc wasn't up to date. Hopefully this explains a little about the differences, and why we made the change :)

@JustinBeckwith JustinBeckwith changed the title TypeError: Cannot read property 'OAuth2' of undefined [Docs] TypeError: Cannot read property 'OAuth2' of undefined Apr 29, 2018

@JustinBeckwith JustinBeckwith added the docs label Apr 29, 2018

@ydancziger

This comment has been minimized.

Copy link

commented May 11, 2018

this is what helped me:
const OAuth2Client = google.google.auth.OAuth2;

@JustinBeckwith

This comment has been minimized.

Copy link
Contributor

commented May 11, 2018

Greetings folks! The quickstart has been updated. Thanks for your patience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.