Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a public calendar using Google API without requiring a user to log in

I'd like to access a public calendar using Google's REST API.

Google's calendar API suggests I need an OAuth token to get access to calendars:

https://developers.google.com/google-apps/calendar/auth

However, I'm accessing a public calendar and am doing this from the server so can't/shouldn't have to ask the user to authenticate.

I'm using the node.js api:

googleapis
  .discover('calendar', 'v3').execute(function(err, client) {
    client.calendar.calendars.get({ calendarId: '***@group.calendar.google.com' })
      .execute(function (err, response) {
        console.log('response from google', response);
      });
  });

This returns "Your client has issued a malformed or illegal request. That’s all we know."

Calling .withApiKey('***') after .calendars.get() returns the same error.

Any suggestions?

like image 952
Alasdair McLeay Avatar asked Aug 05 '13 17:08

Alasdair McLeay


2 Answers

Oh come on guys! You can fetch data without OAuth without requiring a user to login as long as the calendar is set to be public! I've tested it myself.

$.ajax({
  url:"https://www.googleapis.com/calendar/v3/calendars/" + cal_id + "/events?key=" + api_key,
  success: function(data) {
    console.log(data);
  }
    });

Just try it.

The documentation is here https://developers.google.com/google-apps/calendar/v3/reference/events/list#examples

like image 184
Jim Zenn Avatar answered Nov 11 '22 15:11

Jim Zenn


As the documentation states more than once

All requests to the Google Calendar API must be authorized by an authenticated user.

Hence, if you want to use the google calendar API you need an authenticated user. This can be easily achieved using modules like Passport.js and Passport-google-oauth.

However, if you can leave aside the API for a moment and

  • the calendar is public
  • you want read only access

you can easily grab the public address and consume the calendar through ical, xml or html. Have a look at the UK holidays public calendar: http://imgur.com/UGjPtqp

You can access the data publicly from:

  • xml https://www.google.com/calendar/feeds/en.uk%23holiday%40group.v.calendar.google.com/public/basic
  • ical https://www.google.com/calendar/ical/en.uk%23holiday%40group.v.calendar.google.com/public/basic.ics
  • html https://www.google.com/calendar/embed?src=en.uk%23holiday%40group.v.calendar.google.com&ctz=Europe/London

At this point, it's trivial to fetch such resources with node and get the data you want.

like image 32
danielepolencic Avatar answered Nov 11 '22 14:11

danielepolencic