将 Create-react-app 与 googleapis 一起使用时出错

mur*_*phy 5 javascript google-api node.js reactjs create-react-app

我是 React 的新手,我按照 Heroku 的指南使用Create-react-app来创建一个新的React应用程序。我想使用 Google API 通过 Google 的 OAuth2 对用户进行身份验证并获取刷新令牌,因此我按照 Google API 节点客户端上的指南安装了“googleapis”包,npm install googleapis --save但是在使用 导入 googleapis 后import {google} from 'googleapis',出现以下错误命中npm start

Uncaught TypeError: Cannot convert undefined or null to object
at Function.getPrototypeOf (<anonymous>)
at ./node_modules/google-p12-pem/node_modules/pify/index.js.module.exports (index.js:75)
at Object../node_modules/google-p12-pem/build/src/index.js (index.js:6)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../node_modules/gtoken/build/src/index.js (index.js:50)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../node_modules/google-auth-library/build/src/auth/jwtclient.js (jwtclient.js:63)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../node_modules/google-auth-library/build/src/auth/googleauth.js (googleauth.js:60)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../node_modules/google-auth-library/build/src/index.js (index.js:18)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../node_modules/googleapis/build/src/lib/googleapis.js (googleapis.js:60)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../node_modules/googleapis/build/src/index.js (index.js:18)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../src/App.js (App.css?9a66:26)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object../src/index.js (index.css?f255:26)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at fn (bootstrap 8c14a9046e11e185076c:88)
at Object.0 (registerServiceWorker.js:117)
at __webpack_require__ (bootstrap 8c14a9046e11e185076c:678)
at ./node_modules/ansi-regex/index.js.module.exports (bootstrap 8c14a9046e11e185076c:724)
at bootstrap 8c14a9046e11e185076c:724
Run Code Online (Sandbox Code Playgroud)

我错过了什么导致这个错误?

Roh*_*ali 6

您可以使用 npm 模块“gapi-client”而不是“googleapis”。

做一个

npm install gapi-client
Run Code Online (Sandbox Code Playgroud)

在你的代码中使用,

import gapi from 'gapi-client';
Run Code Online (Sandbox Code Playgroud)

它工作正常,您可以使用 Google 示例中提到的相同示例,例如https://developers.google.com/api-client-library/javascript/start/start-js