Skip to content

Accessing Google Calendar API with a token in React #1385

Closed
@artooras

Description

@artooras

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.

Metadata

Metadata

Assignees

Labels

type: questionRequest for information or clarification. Not an issue.web

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions