React中的Google Auth

时间:2019-02-08 08:35:56

标签: javascript reactjs google-oauth

在react中实现google auth时,出现以下错误(在以下屏幕截图中)。

TypeError: window.gapi.init is not a function
(anonymous function)
src/components/GoogleAuth.js:8
   5 | class GoogleAuth extends Component {
   6 |   componentDidMount() {
   7 |     window.gapi.load("client:auth2", () => {
>  8 |       window.gapi
   9 |         .init({
  10 |           clientId:
  11 |             "258474052449-

代码:

  componentDidMount() {
    window.gapi.load("client:auth2", () => {
      window.gapi
        .init({
          clientId:
            "258474052449-vs1334g29cemopfhplff5nqe5l2vshac.apps.googleusercontent.com",
          scope: "email"
        })
        .then(() => {
          window.gapi.client
            .request({
              path:
                "https://people.googleapis.com/v1/people/me?requestMask.includeField=person.names"
            })
            .then(() => {
              this.auth = window.gapi.auth2.getAuthInstance();
              this.onAuthChange(this.auth.isSignedIn.get());
              this.auth.isSignedIn.listen(this.onAuthChange);
            });
        });
    });
  }

Screenshoot

1 个答案:

答案 0 :(得分:2)

Google API docs表明init函数是在client对象上定义的。

您的window.gapi.init应该是window.gapi.client.init