Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing Google Calendar API with a token in React #1385

Closed
artooras opened this issue Oct 13, 2018 · 7 comments
Closed

Accessing Google Calendar API with a token in React #1385

artooras opened this issue Oct 13, 2018 · 7 comments
Assignees
Labels
type: question Request for information or clarification. Not an issue. web

Comments

@artooras
Copy link

I have been searching the web for days trying to find an example of how to properly access Google Calendar API in my React application. Here are a few notes on my setup:

  • I authenticate with Google via Firebase Authentication Google Sign-in successfully (with scope: 'https://www.googleapis.com/auth/calendar') and save the resulting access token in my App.js component.
  • I then pass the token to my Calendar.js component as a prop, where I want to use it in order to access the Google Calendar API.
  • Even though there are plenty of examples of how to inject a script to download gapi from https://apis.google.com/js/api.js, but I don't want to do this due to various CORS warnings I get as a result.
  • Instead, I would prefer to include Google APIs as a module. Is googleapis what I need?

So, in my Calendar.js I know how to get the events from Google Calendar API (because I have previously done it with script injection as mentioned above):

    CALENDAR.events.list({
          'calendarId': 'primary',
          'timeMin': timeMin.toISOString(),
          'timeMax': timeMax.toISOString(),
          'singleEvents': true
        })
        .then(response => {
          let events = []
          response.result.items.forEach(event => {
            events.push({
              title: event.summary,
              start: new Date(event.start.dateTime),
              end: new Date(event.end.dateTime)
            })
          })
          this.setState({events})
        })

but how do I use googleapis to get to the point where I am authenticated with the token that I have (this.props.token) and have a CALENDAR object that I can make a request to?

I would really appreciate some guidance on this.

@JustinBeckwith JustinBeckwith added the triage me I really want to be triaged. label Oct 14, 2018
@ajaaym
Copy link
Contributor

ajaaym commented Oct 16, 2018

@artooras All api has parameter auth which can be any of this string|OAuth2Client|JWT|Compute|UserRefreshClient. what kind of token you will have in this.props.token?

@artooras
Copy link
Author

artooras commented Oct 17, 2018

Hi @ajaaym. I was referring to the Google OAuth token. In my App.js I get the token like this:

      const provider = new firebase.auth.GoogleAuthProvider()
      provider.addScope('https://www.googleapis.com/auth/calendar')

      firebase.auth().signInWithPopup(provider)
        .then(result => {
          result &&
            this.setState({
              user: result.user,
              token: result.credential.accessToken
            })
        }).catch(error => {
          ...
        })

and then I pass it as a prop to my Calendar.js component.

@ajaaym
Copy link
Contributor

ajaaym commented Oct 18, 2018

can you try below?

  const {google} = require('googleapis');
  const oAuth2Client = new google.auth.OAuth2();
  oAuth2Client.setCredentials({access_token:"Token received from firebase"});
  google.options({auth:oAuth2Client});

@artooras
Copy link
Author

Strangely enough, as soon as I add import {google} from 'googleapis' in my Calendar.js, I get an error:

TypeError: can't convert undefined to object

I will note that I am importing this library in my client-sidd React code. Can this be the issue? Is this library perhaps intended only for server-side Node.js implementations?

@JustinBeckwith JustinBeckwith added the 🚨 This issue needs some love. label Oct 18, 2018
@ajaaym
Copy link
Contributor

ajaaym commented Oct 18, 2018

Looks like it needs fs module link here which is not available on client side.

@sduskis
Copy link

sduskis commented Oct 18, 2018

See googleapis/google-cloud-node#531 for more details

@JustinBeckwith JustinBeckwith added type: question Request for information or clarification. Not an issue. and removed 🚨 This issue needs some love. triage me I really want to be triaged. labels Oct 23, 2018
@fhinkel
Copy link
Contributor

fhinkel commented Dec 7, 2020

Greetings, we're closing this. Looks like the issue got resolved. Please let us know if the issue needs to be reopened.

@fhinkel fhinkel closed this as completed Dec 7, 2020
@fhinkel fhinkel self-assigned this Dec 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Request for information or clarification. Not an issue. web
Projects
None yet
Development

No branches or pull requests

5 participants