Google Calendar API gapi.client.init在create-react-app中引发错误

时间:2018-11-26 13:56:58

标签: javascript reactjs google-api create-react-app google-api-client

我正在尝试改编Google日历浏览器quickstart中的代码以在我的React应用程序中使用。每当我尝试运行gapi.client.init方法时,都会引发此错误:

Refused to display 'https://developers.google.com/static/proxy?usegapi=1&jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.en_US.MDhkA3012xc.O%2Fam%3DQQ%2Frt%3Dj%2Fd%3D1%2Frs%3DAGLTcCM6WmePnR12kdbRAwKb1aCuIQXH1Q%2Fm%3D__features__#parent=http%3A%2F%2Flocalhost%3A3000&rpctoken=1847815717' in a frame because it set 'X-Frame-Options' to 'sameorigin'.

我将所有代码都放在下面。我确保将我的血统列入白名单。我不知道除了http://localhost:3000

import React, { Component } from 'react';
import config from '../../config';

class Index extends Component {
  componentDidMount() {
    window.gapi.load('client:auth2', function() {
      window.gapi.client.init({
        apiKey: config.apiKey,
        discoveryDocs: config.discoveryDocs,
        clientId: config.clientId,
        scope: config.scope
      });
    });
  }
  login() {
    console.log('logging in...');
    window.gapi.auth2.getAuthInstance().signIn();
  }
  render() {
    return (
      <div>
        <button type="button" onClick={this.login.bind(this)}>Login with Google</button>
      </div>
    )
  }
}

export default Index;

1 个答案:

答案 0 :(得分:0)

我意识到我的问题是我没有在gapi.client.init()方法中包含回调函数。当我向其中添加一个空的回调函数时,身份验证可以正常工作。

window.gapi.client.init({
  apiKey: config.apiKey,
  discoveryDocs: config.discoveryDocs,
  clientId: config.clientId,
  scope: config.scope
}).then(function(){ //Put anything here; it can be empty });