I am working on a app that allows my users to schedule/reschedule Calendar events using one of my Google calendars, where I don’t need the users to authenticate themselves w
Access Google Calendar API from a PHP application without user consent screen
Not sure if you really want to go through all this ( it's only once :-) ), but here your go...
"A service account is a special type of Google account intended to represent a non-human user that needs to authenticate and be authorized to access data in Google APIs."
The idea is to authorize/share your calendar with a "Service account" (an email address) and authenticate with a "Service account key".
I'll start with the code :
<?php
// read google calendar from php command line application
require_once './google-api-php-client-2.2.3/vendor/autoload.php';
$client = new Google_Client();
$client->addScope("https://www.googleapis.com/auth/calendar.readonly");
$client->setAuthConfig('client_credentials.json');
$service = new Google_Service_Calendar($client);
$calendarList = $service->calendarList->listCalendarList();
while(true) {
foreach ($calendarList->getItems() as $calendarListEntry) {
echo $calendarListEntry->getSummary();
echo "\n------------------------------\n\n";
// get events
$events = $service->events->listEvents($calendarListEntry->id);
foreach ($events->getItems() as $event) {
echo "- " . $event->getSummary() . "\n";
echo "- " . $event->getStart()->getDateTime() . "\n\n";
}
}
$pageToken = $calendarList->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$calendarList = $service->calendarList->listCalendarList($optParams);
} else {
break;
}
}
How to make it work:
From this repo (https://github.com/googleapis/google-api-php-client/releases)
download "google-api-php-client-2.2.3.zip" and unzip "google-api-php-client-2.2.3" in the root of your php application.
Next, you need this "client_credentials.json" file. You'll get it from "Google Cloud Platform". Best would be to create a new project, you could use an existing project but you might not get what I describe below (you might be able to make it, the essence is the same).
After you create and select a new project,
- go to "APIs & Services" > "Library"
- search for "Calendar" then select "Google Calendar API"
- ENABLE
Next, you need to "CREATE CREDENTIALS"
- if you're not already redirected go to "APIs & Services" > "Credentials"
- click on "Create credentials" > "Help me choose"
- they'll ask you "Which API are you using", choose "Google Calendar API"
- next, they'll ask you "Where will you be calling the API from?", choose "Other UI (e.g. Windows, CLI tool)"
- next, they'll ask you "What data will you be accessing?", pick "Application data"
- next, click on "What credentials do I need?"
Now you're on "Add credentials to your project" page
- fill "Service account name"
- select "Role" of "Project" > "Editor"
- "Key type" should be "JSON"
- click "Continue" and you'll be prompted to download this "something-something.json" file (you can save it in the root of your php application)
Next, in your php application folder, rename the "something-something.json" to "client_credentials.json"
Almost there...
Next, open "client_credentials.json" with a text editor
- copy the email address that is the value of "client_email"
something like "test-php-app@xxxxxxxxxx-nnnnnnnnnnnnn.iam.gserviceaccount.com"
Next, go to your calendar (your Google Calendar, the one you want to access) and go to "Settings" > "Calendar settings"
- scroll down to "Share with specific people" (you'll see yourself in the list)
- click on "+ Add people" and paste the email address the one like "test-php-app@xxxxxxxxxx-nnnnnnnnnnnnn.iam.gserviceaccount.com"
- select what you'd like for "Permissions"
- click on "Send" button
Now your php application via the service account has access to your calendar.
Here is the code to show the calendar id:
echo "calendar summary: " . $calendarListEntry->getSummary();
echo "\ncalendar id: " . $calendarListEntry->getId();
echo "\n------------------------------\n\n";
If you're trying to use a Service Account, the answer is here: DalmTo explain in full detail.
Google Calendar API - PHP
I've tried all other examples and none work.