Google drive API - Cannot read property 'OAuth

2019-01-20 19:07发布

问题:

I am following this quickstart tutorial to eventually be able to download certain files from my application.

As I followed through with the tutorial I got the error Cannot read property 'OAuth2' of undefined and it's coming from that line:

const fs = require('fs');
const readline = require('readline');
const google = require('googleapis');
const OAuth2Client = google.auth.OAuth2; << google.auth = undefined
const SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'];
const TOKEN_PATH = 'credentials.json';
// the rest of the code is exactly as it is in the tutorial

I already installed googleapis @ 27 and here's my package.json

{
  "name": "temp-google-drive-api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "googleapis": "^27.0.0"
  }
}

I have already reported that to google but in the interest of hopefully getting this done today has anyone here faced a similar issue?

回答1:

Please try const {google} = require('googleapis');. From v26.0.0, it the usage was changed. https://github.com/google/google-api-nodejs-client/releases/tag/v26.0.0

Unfortunately, after the version of v25.0.0, some bugs for APIs and the options are reported. I believe that these bugs are removed in the future. So if for APIs and the options you use, some errors occur, please modify the version of googleapis, and try again.

References :

  • How do I update my google sheet in v4?
  • Create a gmail filter with Gmail API nodejs, Error: Filter doesn't have any criteria
  • Insufficient Permission when trying to create a folder on Google Drive via API(v3)
  • Youtube Data API V3 - Error fetching video with google.youtube.videos.list()