简体   繁体   中英

Google Calendar API gapi.client.init in create-react-app throws error

I am trying to adapt the code from the Google Calendar browser quickstart to work in my React app. Whenever I try to run the gapi.client.init method, it throws this error:

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'.

I put all my code below. I made sure to whitelist my origin. I don't know if it has to be something besides 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;

I realized my problem was that I didn't include a callback function with the gapi.client.init() method. When I added an empty callback function to it, authentication worked perfectly.

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 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM