I'd like to write some simple PHP code for a website, to get read-only access to a public Google calendar list of events using Google's calendar API V3. I simply need to display a list of upcoming events from the calendar on a web page. I've read the developer docs and looked at the getting started section and the get events example code but I'm a bit confused. I'm wondering:
Is there a simpler way to do what I need that avoids all this OAuth stuff? Is there a non-authenticated access method?
If not, It looks like I need to set up access for my app at code.google.com/apis/console/, and then generate an ApplicationName, ClientID, ClientSecret, RedirectURI and a DeveloperKey. The ApplicationName and DeveloperKey are straightforward enough to find in Google Console. But for the other items I need to create an OAuth 2.0 ClientID, which requires me to choose if the Application Type is a Web Application or a Service Account. What should it be for my scenario? In this scenario no user login should be required, as I'm not requesting access to their calendar-- it's just a public calendar I need access to.
What is the Redirect URI and how does it get used. How should I set it up?
Google's API sample gets and caches an Access Token in Session data. Then the code comments say not to do this, that you should use a database instead. Seriously? Do I really need to deal with caching the access token? If so, is there a built-in DB caching mechanism in Google API or how do you handle this?
I'm really looking for a simpler approach. Your guidance would be welcome!
Thanks.
I think you're way overthinking this one. If you just need access to events on a public calendar, then there's a way to do this without invoking the Google Calendar API and all of the attached complexity. Every public Google Calendar has an iCal feed that can be viewed at a specific URL, usually in the form of:
http://www.google.com/calendar/ical/{OWNER}/public/basic.ics
where {OWNER}
is the URL-encoded email address of the owner of the calendar.
You can find the specific URL by viewing the calendar settings and scrolling to the bottom of the Calendar Details tab.
Some tips to do this are embedded in the comments to Charles' answer. Anyway, as of 2014 (with Google Calendar v3 API), this is what I did:
Go here to see how the v3 API works:
https://developers.google.com/google-apps/calendar/v3/reference/events/list#try-it
Fill at least these three fields in the demo web form available on the above link:calendarId
: the URL-encoded email address of the owner of the calendar.timeMax
: End TimetimeMin
: Start Time
(Use this Time Format: 2014-05-04T00:00:00-00:00
(Y-m-d T millisecond - offset))
You'll see the following REQUEST being sent out.
GET https://www.googleapis.com/calendar/v3/calendars/{CALENDER_ID}/events?timeMax={start_time}&timeMin={end_time}&key={YOUR_API_KEY}
Now you can customize this request URL for your requirement and send regular GET requests from your code.
P.S -- To get your API Key, follow the procedure here:
https://developers.google.com/google-apps/calendar/auth
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With