使用带有googleapis的Create-react-app时出错

时间:2018-02-15 05:30:21

标签: javascript node.js reactjs google-api create-react-app

我是React的新手,我按照Heroku的指南使用创建反应应用来创建新的反应应用。我想使用Google API通过Google的OAuth2对用户进行身份验证并获取刷新令牌,因此我按照Google API节点客户端上的指南安装了' googleapis'使用npm install googleapis --save打包但在我使用import {google} from 'googleapis'导入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

我错过了什么导致此错误?

2 个答案:

答案 0 :(得分:4)

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

做一个

npm install gapi-client

并在您的代码中使用like,

import gapi from 'gapi-client';

它工作正常,你可以使用谷歌样本中提到的相同例子,如https://developers.google.com/api-client-library/javascript/start/start-js

答案 1 :(得分:1)

此处的错误是将其呈现在客户端上,而不是服务器端上。 googleapis库不是要在浏览器上运行,而是在节点应用程序服务器中运行。

Here is the full answer (github)