Last active
August 29, 2015 14:16
-
-
Save moust/6eae6b2409c2ff69d2fb to your computer and use it in GitHub Desktop.
Google Calendar API connection with private key file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_once 'vendor/google-api-php-client/src/Google/autoload.php'; | |
// email address from the client | |
$serviceAccountName = '####################@developer.gserviceaccount.com'; | |
$scopes = array( | |
'https://www.googleapis.com/auth/calendar', | |
'https://www.googleapis.com/auth/calendar.readonly' | |
); | |
$privateKey = file_get_contents(__DIR__.'/private-key.p12'); | |
$credentials = new Google_Auth_AssertionCredentials($serviceAccountName, $scopes, $privateKey); | |
$client = new Google_Client(); | |
$client->setApplicationName('My Application'); | |
$client->setAssertionCredentials($credentials); | |
if( $client->getAuth()->isAccessTokenExpired() ) { | |
$client->getAuth()->refreshTokenWithAssertion($credentials); | |
} | |
$service = new Google_Service_Calendar($client); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Example of how to get list of events for each calendars shared through the api | |
*/ | |
$calendars = $service->calendarList->listCalendarList(); | |
foreach ($calendars->getItems() as $calendar) { | |
echo '<p><strong>' . $calendar->getSummary() . '</strong> ' . $calendar->getId() . '</p>'; | |
$params = array( | |
'singleEvents' => true, | |
'orderBy' => 'startTime', | |
'timeMin' => (new DateTime())->format(DateTime::RFC3339), | |
'timeMax' => (new DateTime())->add(new DateInterval('P1D'))->format(DateTime::RFC3339), | |
); | |
$events = $service->events->listEvents( $calendar->getId(), $params ); | |
foreach ($events->getItems() as $event) { | |
echo '<p>'; | |
echo "Summary: ", $event->getSummary(), "<br />"; | |
echo "Location: ", $event->getLocation(), "<br />"; | |
echo "Start: ", $event->getStart()['date'], "<br />"; | |
echo "End: ", $event->getEnd()['date'], "<br />"; | |
echo '</p>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment