0

I'm trying to use the google Calendar API to get all events from a G-Calendar. The following code works with Node.js

const {google} = require('googleapis');

const auth = new google.auth.JWT(
    CREDENTIALS.client_email,
    null,
    CREDENTIALS.private_key,
    SCOPES
);

const getEvents = async (dateTimeStart, dateTimeEnd) => {

    try {
        let response = await calendar.events.list({
            auth: auth,
            calendarId: calendarId,
            timeMin: dateTimeStart,
            timeMax: dateTimeEnd,
            timeZone: 'Asia/Kolkata'
        });
    
        let items = response['data']['items'];
        return items;
    } catch (error) {
        console.log(`Error at getEvents --> ${error}`);
        return 0;
    }
};

But because I can't use the "require" in normal browser javascript it doesn't work. I included <script src="https://apis.google.com/js/api.js"></script> and const google = gapi; instead of the const {google} = require('googleapis');

That in itself works but it does not recognize const auth = new google.auth.JWT() as a function anymore and throws an Uncaught TypeError: google.auth is undefined

I would really appreachiate if someone could help. Thank you all in advance!

  • Does anybody have a hint? – Jonathan Schmidt Dec 16 '21 at 09:35
  • Node is for server JS, have you tried implementing it following the [Javascript Quickstart](https://developers.google.com/calendar/api/quickstart/js)? – Kessy Dec 16 '21 at 10:55
  • @Kessy I know the quickstart guide. The issue is, that this is only user authenticated and I need to auth with a service account because I just wanna get all events from a calendar for a custom calendar javascript frontend app and some users might not have an account. – Jonathan Schmidt Dec 16 '21 at 15:30
  • It's still an issue if someone has an idea it would be great! – Jonathan Schmidt Dec 19 '21 at 19:03

1 Answers1

0

If you need a client-side implementation for "normal browser javascript", you cannot use the node.js implementation

Instead you would need to create a flow to obtain a Google service account access token. There is a good sample of how to implement it with XHR:

https://stackoverflow.com/a/29288013/11599789

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • Thank you very much for your answer. I got a lot further, so far I can get an access token from the authentication service back but **I still struggle to access my calendar.**
    In node.js I would just use `const calendar = google.calendar({version : "v3"});` and then later `calendar.events.list({...})` but all this isn't working with pure browser javascript. It doesn't recognizes the first part and when I try `gapi.calendar.events` it throws `TypeError: gapi.calendar is undefined` back. **Did I forget to import something?** I used `https://apis.google.com/js/api.js`
    – Jonathan Schmidt Dec 21 '21 at 00:09
  • Browser Javascript implementation for the Calendar API is very different. You need to start with the [quickstart](https://developers.google.com/calendar/api/quickstart/js) as a base to define gapi. – ziganotschka Dec 21 '21 at 08:07
  • yeah but thats only for a sign-in process and not for a service account token. How do I combine these two? And It's not even working when I just copied it and tried a run. It's really frustrating to work with an unclear documentation. – Jonathan Schmidt Dec 21 '21 at 14:50
  • How do you have the script after this answer? Can you update the question with the script you have now? – Kessy Dec 24 '21 at 13:55