Viking NM
Viking NM

Reputation: 402

Google Calendar API v3 PHP and JSON

Have a public Google Calendar where all I want to do is get the events from "today" up to n (or max) events. I've been running around in circles trying to figure it out, it's either depretiated information or the soutions don't seem to work. A good solution path is using PHP to generate the initial list of events with css goodness and javascript array so I can update a details DIV when the user clicks on an event.

Using PHP and uploaded the latest API to the server, have a calendar project with a read only account, an OAuth 2.0 client ID, and API key. Got close with a few tutorials but hit a snag because you need the './credentials/calendar-api-quickstart.json' which is generated through the command line. The server I have to use is a shared one so there is no command line access (for obvious reasons :)). Used the https://developers.google.com/google-apps/calendar/quickstart/php information. That seems to be the the only piece i'm missing?

Activated the key too through the info on this page Google Calendar API v3 hardcoded credentials but getting an "invalid_grant" error. I've tried everything that came up in search but nothing seems to work. Is there an accurate up to date tutorial (2016-2017) that walks you though getting the Google Calendar data into your site? From setting up the project to oauth2 and token generation. I've got to be messing something up because nothing seems to work.

The only other solution is to have them export the calendar as an .ics but they want live updates as they update the google calendar.

Because i'm just looking to read the data I tried the suggestion here Get JSON from a public Google Calendar. But I get the Forbidden Error 403 error so it's depretiated?

Still don't understand why they made it this complicated...I understand going through all the oauth stuff if you want to interact with the calendar on your site or app but just to read the data on a public calendar (which warns you making it public that everyone can see it)?

Thank you

Upvotes: 1

Views: 4524

Answers (1)

Viking NM
Viking NM

Reputation: 402

Finally found the solution. Posting here and might make a full tutorial later if i get any free time :). Hopefully it helps others.

https://console.developers.google.com/iam-admin/projects create a project which should take you to the console after creating it...if not here is the console link https://console.developers.google.com/

in the library section enable the "Google Calendar API". Search for Google Calendar API, select it and use the enable button that appears at the top.

Go to the credentials section and add a Service account key. New service account (any name you want).

Rolls:

Project > Viewer

App Engine > App Engine Viewer

Storage > Storage Object Viewer

You might not need all those but that seems to be the combonation that worked for me. Make sure JSON is selected then on create it'll download a .json file. It only downloads it once with no way to redownload so make sure you download and save it somewhere safe.

Download the latest API for PHP here https://developers.google.com/google-apps/calendar/downloads and upload it to wherever on your site. Fairly large with lots of support files so may take a while to upload.

Then this is the PHP code that worked for me :)

CALENDARID is the id of your calendar... from your google calendar, use the popup and choose "calendar settings". Then towards the bottom in the "Calendar Address:" section make note of the Calendar ID. should be some kind of google .com email address or your email address if you set it up through the gSuite tools.

"YOURJSON.json" is the name of the json file you downloaded earlier. Suggestion to keep it safer is to upload it above the root directory on your site and link to it from there. Since it only has read roles it shouldn't cause any problems so more of a sanity just in case thing.

require_once line is wherever you uploaded the google API to your server. Don't forget the /vendor/autoload.php part :).

This is the solution that worked for me (2017-04-24). The last line of the PHP code is a general echo command so you can see the array that you get and all the data available. The only thing I don't see coming over is the custom color for an event. There is a space for it but doesn't seem to come over.

<?php
$maxEvents = 100;
$minStartDate = date('c');
$calendarId = 'CALENDARID';

//path to the google API on your server
require_once 'inc/google-api-php-client-2.1.3_PHP54/vendor/autoload.php';
//set environment variable to use your downloaded Service account key
putenv("GOOGLE_APPLICATION_CREDENTIALS=YOURJSON.json");
$scope = 'https://www.googleapis.com/auth/calendar.readonly';

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes($scope);
$service = new Google_Service_Calendar($client);

//options link
//  https://developers.google.com/google-apps/calendar/v3/reference/events/list
$options = array(
    'maxResults' => $maxEvents,
    'orderBy' => 'startTime',
    'singleEvents' => TRUE,
    //UNIX timestamp format
    'timeMin' => $minStartDate,
    //to use a calendar other than the default uncomment and enter the calendar's ID
    //not really needed here since you're using the $calendarId but does pull another calendar and more for completeness
    //'iCalUID' => 'CAL_ID_FROM_GOOGLE_CALENDAR'
);

$results = $service->events->listEvents($calendarId, $options);
//echo 'results<br><pre>';print_r($results); echo '</pre><br>';
?>

Upvotes: 2

Related Questions