Are you working on a app that allows your users to schedule/reschedule Calendar events using one of your Google calendars, you I don’t need the users to authenticate themselves with Google. What you need to use is the Google Calendar API with a service account.
In this tutorial we are going to go though the steps needed to connect to the Google Calendar API with a service account.
Accessing the Google APIs using a service account is quite useful. Some times you just want to access your own data and not data owned by other users. In this instance there is no reason to use OAuth2 and prompt a user to give you access to there information, its your information you already have access. This is why we use a service account. This tutorial uses the Google Calendar API to walk you though the steps of creating a service account and how to then access your Google Calendar with it.
[wp_ad_camp_3]
What is a Service Account.
A service account will allow your application to access your Google Calendar without prompting a user for access. A service account is essentially Oauth1. Oauth2 is when a user is prompted to give your application permission to use it. If you are going to only be access your own data then you should be using a Service Account. If you want to access data owned by another user then you should be using Oauth2 and looking at a different tutorial. You can find it here {Adding soon}
PHP client library
In this tutorial we will be using the newest version of the Google Client library for PHP it can be found on Github. The google-api-php-client client library is updated regularly whenever anything changes. You will need to copy the entire src/Google directory to the directory of your application. I don’t recommend only taking the files that you need unless you really know what you are doing.
Google Developers Console
[wp_ad_camp_1]Before you can access any of the Google APIs you need to first create your application in the Google Developers console. I have a separate tutorial that goes though the steps of creating an application on the Google Developers console you can find it here. Google Cloud Console Once you have completed the basic set up including selecting which API that you want to access. For this tutorial we will be using Google Calendar . Make sure you have filed in both an Email Address and a product name under the consent screen or it wont work.
Creating Service Account
On the Credentials screen you will see a button that says Create new clientid, click on it. In the popup window selected the “Service account” radio button. A file will begin to download you will need this file later. Copy the Email Address that was created for you it should look something like this: 1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp@developer.gserviceaccount.com
Granting Access
A service account doesn’t need to prompt a user for access because you have to set it up. Go to the Google Calendar website. Find the Calendar Settings , then go to the Calendars tab, find the calendar you want to access and click on “Shared: Edit settings” add the service account email address like you would a persons email address. This will give the service account the same access as if you where sharing it with any other user.
The Code
setApplicationName("Client_Library_Examples"); $key = file_get_contents($key_file_location); // separate additional scopes with a comma $scopes ="https://www.googleapis.com/auth/calendar.readonly"; $cred = new Google_Auth_AssertionCredentials( $Email_address, array($scopes), $key ); $client->setAssertionCredentials($cred); if($client->getAuth()->isAccessTokenExpired()) { $client->getAuth()->refreshTokenWithAssertion($cred); } $service = new Google_Service_Calendar($client); ?> calendarList->listCalendarList(); while(true) { foreach ($calendarList->getItems() as $calendarListEntry) { echo $calendarListEntry->getSummary()."
\n"; // get events $events = $service->events->listEvents($calendarListEntry->id); foreach ($events->getItems() as $event) { echo "-----".$event->getSummary()."
"; } } $pageToken = $calendarList->getNextPageToken(); if ($pageToken) { $optParams = array('pageToken' => $pageToken); $calendarList = $service->calendarList->listCalendarList($optParams); } else { break; } } ?>
Conclusion
Using a service account to access your Google Calendar data can be every useful if you want to allow a number of people to access the same calendar. By using a service account you remove the need to prompt a user to give your application access, as long as the application email has access in the Google calendar this script will work without authentication prompt.
Working Version of this script can be found here: Google Calendar Service Account PHP
I Linda. I really need yout help. I wanted to make a small form to put a simple informacion on Google Calender. Is for a school project where I ‘ll mark meetings for teachers and the meet is automatically go to GC.
I read some information and dont understand anything.
Can you help me please
PS: Sorry for my english… I portugues. Thanks for reading
I think using a service account is a very good idea. You can have your form make changes to the calendar. But the question will be how best to show the calendars to the teachers. You will need to ensure that they either all have access to the calendar in question or make a list as part of a web page some where.
Your English was fine 🙂
thank you for helping.
I gone use the code in this page.
But, to insert the informacion in GC… What I do?
One more time, thank you.
I put all code and the output is this
Google_Service_Calendar_CalendarList Object ( [collection_key:protected] => items [internal_gapi_mappings:protected] => Array ( ) [etag] => “1418644240421000” [itemsType:protected] => Google_Service_Calendar_CalendarListEntry [itemsDataType:protected] => array [kind] => calendar#calendarList [nextPageToken] => [nextSyncToken] => 00001418644240421000 [modelData:protected] => Array ( [items] => Array ( ) ) [processed:protected] => Array ( ) )
Something wrong? Or is ok?
To answer your question, the idea is put the preset HTML calendar.
like : <iframe src…
My Idea is use the email account of webmaster of school to mark all meething.
thanks for helping me
i really need =)
Not sure i understand what you mean about using “email account of webmaster of school” Remember you need to use Open Authentication.
That looks ok to me you are getting a list of the calendars.
My colleague allready do an email service for mark metting. But now I want use the GC service because all teachers use. So we create a “webmaster” email to do this settings thing like send an email. This email are the mail of the calendar.
Open authentication? I do… I think =s
I apologize if I’m being boring by not realize any of this … If you have availability we can talk via email where I can explain better since I’m really lost …
My mail : joaopmcalado@gmail.com
Thank you for everything
If you have a project you need help with I would be happy to give you a hand there is a contact from on the site here: https://www.daimto.com/consulting-service/
I have been trying to get this to work since API v2 was deprecated last month and my Zend Framework connection no longer works.
I am not sure what I am missing.
I have signed up for a service acct and DL the key and have it on the webserver.
I have the calendar acct shared with the service acct email as you mentioned.
I DL the most recent version of the php-client library at Github and have it in the appropriate file.
I used your code with the appropriate substitutions and get this output:
Google_Service_Calendar_CalendarList Object ( [collection_key:protected] => items [internal_gapi_mappings:protected] => Array ( ) [etag] => “1418630976822000” [itemsType:protected] => Google_Service_Calendar_CalendarListEntry [itemsDataType:protected] => array [kind] => calendar#calendarList [nextPageToken] => [nextSyncToken] => 00001418630976822000 [modelData:protected] => Array ( [items] => Array ( ) ) [processed:protected] => Array ( ) )
Any suggestions of what I might have forgotten?
I would be happy to buy you a month of coffee if you can figure this out
It looks like you have access. How many items are getting returned? Have you tried looping though the calendar items?
Wow you are right!!!
I just tried adding an event and it worked. If you get a chance tell me what you think of the code. I am a total amateur – Anesthesiologist in real life.
I dropped the ‘readaonly’ from scopes and added the add event code.
$client_id = ‘684605746077-c3rkjhan7l06sMY_CLIENT_ID.apps.googleusercontent.com’;
$Email_address = ‘684605746077-c3rkj_MY_SERVICE_EMAIL_q8r6@developer.gserviceaccount.com’;
$key_file_location = ‘./Calen_MY_KEY_FILE_LOCATIONe8.p12’;
$client = new Google_Client();
$client->setApplicationName(“CalendarTHree”);
$key = file_get_contents($key_file_location);
// seproate additional scopes with a comma
$scopes =”https://www.googleapis.com/auth/calendar”;
$cred = new Google_Auth_AssertionCredentials(
$Email_address,
array($scopes),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$service = new Google_Service_Calendar($client);
?>
setSummary(‘Event 2’);
$event->setLocation(‘Somewhere’);
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime(‘2015-06-22T19:00:00.000+01:00’);
$start->setTimeZone(‘Europe/London’);
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime(‘2015-06-22T19:25:00.000+01:00’);
$end->setTimeZone(‘Europe/London’);
$event->setEnd($end);
//
$calendar_id = “nmp0idjes0j03tg4uq65akhak0@group.calendar.google.com”;
//
$new_event = null;
//
try {
$new_event = $service->events->insert($calendar_id, $event);
//
$new_event_id= $new_event->getId();
} catch (Google_ServiceException $e) {
syslog(LOG_ERR, $e->getMessage());
}
//
$event = $service->events->get($calendar_id, $new_event->getId());
//
if ($event != null) {
echo “Inserted:”;
echo “EventID=”.$event->getId();
echo “Summary=”.$event->getSummary();
echo “Status=”.$event->getStatus();
}
?>
PS Your coffee is on the way – cheers
I try your code but give me a error about REST and RESTful
How to fix that???
My output by the way:
Fatal error: Uncaught exception ‘Google_Service_Exception’ with message ‘Error calling POST https://www.googleapis.com/calendar/v3/calendars/espamol.pt_09rcn73rq50i16mege2rroj9qs%40group.calendar.google.com/events: (403) Forbidden’ in /home/luiscon1/public_html/gstor/paa/GoogleAPI/google-api-php-client/src/Google/Http/REST.php:83 Stack trace: #0 /home/luiscon1/public_html/gstor/paa/GoogleAPI/google-api-php-client/src/Google/Http/REST.php(41): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client)) #1 /home/luiscon1/public_html/gstor/paa/GoogleAPI/google-api-php-client/src/Google/Client.php(564): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request)) #2 /home/luiscon1/public_html/gstor/paa/GoogleAPI/google-api-php-client/src/Google/Service/Resource.php(227): Google_Client->execute(Object(Google_Http_Request)) #3 /home/luiscon1/public_html/gstor/paa/GoogleAPI/google-api-php-client/src/Google/Service/Calendar.php(1440): Google_Service_Resource->call(‘insert’, Array, ‘Go in /home/luiscon1/public_html/gstor/paa/GoogleAPI/google-api-php-client/src/Google/Http/REST.php on line 83.
and when i try to implement on localhost your code example it returns:
Fatal error: Uncaught exception ‘Google_Auth_Exception’ with message ‘Could not json decode the token’ in K:\home\localhost\www\shop\modules\gcalendarv3\api\Google\Auth\OAuth2.php:180 Stack trace: #0 K:\home\localhost\www\shop\modules\gcalendarv3\api\Google\Auth\OAuth2.php(295): Google_Auth_OAuth2->setAccessToken(‘[]’) #1 K:\home\localhost\www\shop\modules\gcalendarv3\test0.php(32): Google_Auth_OAuth2->refreshTokenWithAssertion(Object(Google_Auth_AssertionCredentials)) #2 {main} thrown in K:\home\localhost\www\shop\modules\gcalendarv3\api\Google\Auth\OAuth2.php on line 180
Try and get the NuGet package again looks like somethings wrong.
Thans, for the reply.
I regenerated the p12 fie and there were no error on authentication.
Now there’s another problem:
for code:
setApplicationName(“landshaftinfo”);
if (isset($_SESSION[‘service_token’])) {
$client->setAccessToken($_SESSION[‘service_token’]);
}
$key = file_get_contents(dirname(__FILE__) . ‘/api/Google/’ .$key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array(‘https://www.googleapis.com/auth/calendar’),
$key
);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
try {
$client->getAuth()->refreshTokenWithAssertion($cred);
} catch (Exception $e) {
var_dump($e->getMessage());
}
}
$service = new Google_Service_Calendar($client);
$_SESSION[‘service_token’] = $client->getAccessToken();
$event = new Google_Service_Calendar_Event();
$event->setSummary(‘Appointment’);
$event->setLocation(‘Somewhere’);
$event->setDescription(‘Description’);
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime(‘2015-02-24T13:00:00.000+02:00’);
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime(‘2015-02-24T13:20:00.000+02:00’);
$event->setEnd($end);
$reminders = new Google_Service_Calendar_EventReminders();
$reminders->setUseDefault(false);
$overrides = array(“method”=> “sms”,”minutes” => “0”);
$reminders->setOverrides(array($overrides));
$event->setReminders($reminders);
$createdEvent = $service->events->insert(‘primary’, $event);
echo “Id=”.$createdEvent->getId();
echo “Summary=”.$createdEvent->getSummary();
echo “Status=”.$createdEvent->getStatus();
echo “Created=”.$createdEvent->getCreated();
$start = new Google_Service_Calendar_EventDateTime();
$createdEvent->GetStart($start);
echo $start->getDateTime;
$end = new Google_Service_Calendar_EventDateTime();
$createdEvent->GetEnd($end);
echo $end->getDateTime;
?>
i get something like:
Id=qq5mu6efor3qhtb79192kd7s6cSummary=AppointmentStatus=confirmedCreated=2015-02-25T17:55:50.000Z
i have event id and date of creation BUT no start date or end date, and no event is shown on calendar.
figured out – it was the
$createdEvent = $service->events->insert(‘pimary’, $event);
it has to be
$createdEvent = $service->events->insert(”, $event);
then the event will appear in calendar.
Now there’s only left to make it work with sms-reminder.
Hmm, seems like service account cant override reminders.
The way to get sms reminder how i did it:
1)create one more calendar
2)share it with service acccont
3)set for this calendar sms reminding as default
then there is no need to override it in script
Hey Linda, your post helped me a lot, thank you.
Would you like more a help, is there any way to include an alias for the e-mail address in the service account?
When the event is created in designer details:
Created by 558943131-o3evlg5a7ha8caabbpooddsj1mjv4sqmd@developer.gserviceaccount.com
And this email to a customer view is not legal.
example alias: serviceschedule@empresa.com
Very very thanks.
No I don’t think so. A Service account email isn’t really an email I think its more a unique identifier for the service account. Wild guess is that it has an email format so that we can add it as a user to say Google Analytics, Drive and Google calendar. These systems probably require that adding a user be email format string.
Thank you Linda, I believe it is just that.
See you.
Hello Linda,
Nice tutorials!
But I’m stuck on this one for several hours now.
I created a service account and shared my calendar with it. The access was granted, but it returns an empty array, when I do print_r($calendarLists->getItems()).
Meaning that I don’t receive any events from the calendar.
Any idea what could be the cause of this?
P.S. The calendar is public.
I think a calendar needs to be first added to the Calendarlist for a service account. So you need to find the Calendar Id in Google Calendar web version and use that to add it to the Calendar list. I will need to test this some more.
Found one problem.
First of all I need to take the event list. 😀
Mrs. Linda,
I enjoyed the tutorial and it helped me understand it easier. Everything else I had were all fragmented pieces of this.
When I changed the creds in the code with mine, it is giving me an error code of:
“Fatal error: Uncaught exception ‘Google_Exception’ with message ‘The Google PHP API library needs the openssl PHP extension’ in C:\wamp\www\newOLP\scripts\oAuth2\Google\Signer\P12.php on line 38”
I am using the .p12 file. I first attempted with the JSON file and read the comments for others to see if they had the same error I have. I’m not sure what the error exactly means anymore after I thought I did what I thought it meant, but to no success I undid the changes.
Thanks for any help,
Matthew
Ahaha, I just found it out. I forgot to restart my server when I enabled my openssl.
Any idea how to use this same example to access a Google Spreadsheet? I’ve been going in circles trying to find it. It does not seem that the Google API PHP Client has access to spreadsheets. I found this:
https://github.com/NoProtocol/google-spreadsheet
But I don’t know how to integrate it into your example.
Any help appreciated.
I think everyone is looking for that right now. I am still working on something.
Is that possible to use google calender with all features in my website if yes then how ?
If you mean embed Google Calendar in your website probably not. You could in theory develop an application that functions similar to Google calendar and put that on your website. That would be a very big project.
Great Article!
Nothing more to say 🙂
print_r($calendarLists->getItems()). Its coming empty why, I have created service account and shared with that email id and calendar but still that is coming empty, can yu tell me what should i need to do.
If I understand you correctly you have shared one of your calendars with the service account, good job most people forget this step.
Now before a calendar can appear in calendarList it has to be add to calendarList. Take the calendar id and insert it to the calendar list then you can list it.
Now technically speaking you don’t need to add it to calendar list. You could just do a calendar.get on it you should know the id of the calendar.
What does “insert it to the calendar list” mean code-wise?
I dont normally use calendar list in a service account I dont fined a need for it really.
Hi Linda,
Thank you so much for this article – after 5hrs of getting nowhere – I can now access my calendar through php!
However, I still have one problem, which I hope you can help with. The above script will not display on my website (view page source is blank) – but I can pipe it into a html file which I can then view in browser. What am I doing wrong/missing?
Thanks for your help!
Paul
Good question are you sure it has access to the key file? try and print out a few messages see where its stopping.
Hi, I was recently able to get the Google API to work using a service account following your Analytics tutorial (https://www.daimto.com/google_service_account_php/). My end goal is to work with the Google Calendar API, which is why i am now going through this tutorial. I’ve followed your code and am able to connect just fine. No error messages.
My problem is that no calendars are listed. I’m not getting any output.
In the following example, “IN WHILE TRUE” echos. But “IN FOR EACH” does not.
I’m at a loss. any tips or points in the right direction would be very much appreciated!
$calendarList = $service->calendarList->listCalendarList();
while(true) {
echo “IN WHILE TRUE”;
foreach ($calendarList->getItems() as $calendarListEntry) {
echo “IN FOR EACH”;
echo $calendarListEntry->getSummary().”\n”;
// get events
$events = $service->events->listEvents($calendarListEntry->id);
foreach ($events->getItems() as $event) {
echo “—–“.$event->getSummary().””;
}
}
$pageToken = $calendarList->getNextPageToken();
if ($pageToken) {
$optParams = array(‘pageToken’ => $pageToken);
$calendarList = $service->calendarList->listCalendarList($optParams);
} else {
break;
}
}
By default a service account doesn’t have access to any Calendars. You need to either create one using Calendars: insert or grant it access to one of your own calendars. Try reading this Google Calendar API Service Account Authentication it tells you how to give a service account access to a Google calendar.
Also, as a note: it appears that “// separate additional scopes with a comma” produces an error. I was able to get this to work when separating additional scopes with a space. Is that right?
Use a comma
string[] scopes = new string[] {
CalendarService.Scope.Calendar , // Manage your calendars
CalendarService.Scope.CalendarReadonly // View your Calendars
};
This is not work for me please help me. It shows Following Error
Fatal error: Uncaught exception ‘Exception’ with message ‘This library must be installed via composer or by downloading the full package. See the instructions at https://github.com/google/google-api-php-client#installation.’ in /home/sadgurusoft1992/public_html/demo/cal/Google/autoload.php:14 Stack trace: #0 /home/sadgurusoft1992/public_html/demo/cal/index.php(2): require_once() #1 {main} thrown in /home/sadgurusoft1992/public_html/demo/cal/Google/autoload.php on line 14
you need to either use composer or grab the master-v1 branch of the client library
Great tutorial and I have got the calendar API working at home but when I brought the files into work we have a Proxy server and I just get net::ERR_Connection_refused.
What do I need to do to get through the proxy with authentication?
Unfortunately i don’t have any experience with proxy servers. Try and post a question on stackoverflow.com
i was add event to the google calender using google api with service account problem is when i was add event then event successfully inserted but they not showing in google calander please help me how to fix??
Double check that you are adding it to the correct calendar remember a service account has its own calendar account. You need to be adding it to one of your personal calendars not the service accounts calendar.
Tutorial could use a little more indepth information in the how-to section….
I have entered the following variables:
$client_id: I assume that this is the clientID mentioned at OAuth 2.0 client IDs in the console??? Maybe not, there is no explanation in this tutorial….
$Email_address: I took the one that was mentioned in the downloaded JSON file, the one that ended with “gserviceaccount.com”.
$key_file_location: the path to the downloaded JSON file? I assume so….
And then there is the error message: Fatal error: Uncaught exception ‘Google_Auth_Exception’ with message ‘Unable to load private key’ in….
I think you have miss read the tutorial. This tutorial is for service accounts not Oauth2. The file you download for service accounts is a p12 not Json. Service accounts use service account email not client id.
To use a service account you need to create service account credentials you appear to be trying to use OAuth2 credentials.
I have a PHP Google Calendar tutorial that uses Oauth2 here.
I have already integrated auth2 lib for login user into my application. Can my web application access calendar data without redirect user to go on google.
Yes assuming that you authenticated the user and requested access to there google calendar data then the refresh token you got from login will also work for accessing Google calendar data.
Hi Linda,
I followed this tutorial to create a site with the possibility of add, update events with the idea to integrate it to a joomla component.
I am using my gmail account to access de google development api and my first intention was to use OAuth2 to allow users to add,view and update events but it would not work do to the fact that they would need a google or gmail account. I saw this other option with service account but I am stuck.
considering the next info, could you tell me , what I am doing wrong?
I created a new project on google developer console and I only created the “Service account keys”, only one credential.
$client_id = ‘xxxx@appspot.gserviceaccount.com’; I am using the info from service accounts / Service Account ID
$Email_address = ‘mymail@gmail.com’;
$key_file_location = realpath(dirname(__FILE__) . ‘/../src/Google/xxxxxx.p12’); it create a file and I named it xxxxxx.p12 and is located in localhost/appfolder/google…/src/Google/.
This is the error sent by safari:
Fatal error: Uncaught exception ‘Google_Auth_Exception’ with message ‘Error refreshing the OAuth2 token, message: ‘{ “error” : “invalid_client”, “error_description” : “The OAuth client was invalid.” }” in /Library/WebServer/Documents/fullcalendar/google-api-php-client-master/src/Google/Auth/OAuth2.php:364 Stack trace: #0 /Library/WebServer/Documents/fullcalendar/google-api-php-client-master/src/Google/Auth/OAuth2.php(315): Google_Auth_OAuth2->refreshTokenRequest(Array) #1 /Library/WebServer/Documents/fullcalendar/google-api-php-client-master/examples/Service.php(42): Google_Auth_OAuth2->refreshTokenWithAssertion(Object(Google_Auth_AssertionCredentials)) #2 {main} thrown in /Library/WebServer/Documents/fullcalendar/google-api-php-client-master/src/Google/Auth/OAuth2.php on line 364
Thanks.
Invalid client means there is something wrong with the client id or how you have set it up on Google Developers console. Email address should be the service accounts email address not your gmail address.
Thank you very much for the response, do you have an screen shoot to see from where i should get those values? or any link to a tutorial. As you say, google info is not very explicit.
Thanks.
you might want to try reading this tutorial.
I made a few changes, according to google documentation.
Old code
$client_id = ‘xxxx@appspot.gserviceaccount.com’; I am using the info from service accounts / Service Account ID
$Email_address = ‘mymail@gmail.com’;
New Code
/*$client_id = ‘xxxx@appspot.gserviceaccount.com’;*/ I Commented the line of code
$Email_address = ‘xxxx@appspot.gserviceaccount.com’; Changed client id to Email_Address.
This change causes no error in authentication but the app is not getting any calendar info. In local apache web server.
But I get the next message in online server.
Fatal error: Class ‘Google_Config’ not found in /home/jruelas/public_html/fullcalendar/google-api-php-client-master/src/Google/Client.php on line 80
Hi Linda.
I final got it done. I can create,delete and update calendars. I suppose that it will be the same for events, notifications or any thing else. I have just two questions.
1.- On your tutorials you only use 1 scope. for example.
$scopes =”https://www.googleapis.com/auth/calendar.readonly”;
latter in the code is array($scopes) so it would be Ok $scopes =array(“https://www.googleapis.com/auth/calendar.readonly”,”https://www.googleapis.com/auth/calendar”);
2.- Is there a way to add those calendars to my phone? I have the email, but no password.
I would really appreciate any response. I thank you very much for the tutorials
If you have authenticated then you should have a refresh token. You could grant yourself permissions to see the calendar in question using code.
Yes you can add more then one scope but there really isn’t a point for both readonly and calendar as calendar will give you everything anyway. Its used more for requesting access to say google drive and google calendar assuming you need access to both of those APIs.
Managed to get a working solution, see How to create a list view of Google Calendar with PHP
I am confused on how to work this .p12 file format that everyone is talking about here. When I create it, there is no email address displayed anywhere and the file that is downloaded is a certificate that does not contain an email address in it. If I use the JSON file format, I can find the email address, but apparently you don’t use the JSON file format for Server to Server according to the answers I saw here. I am totally lost on what to do next.
the service account email address is on Google developers console. look for Service account ID.
Linda, thanks for all of the great documentation of Google APIs using PHP
I see Google rolling out something called Firebreak (for OAuth and multiple service signon), but I don’t see any PHP support / example code from them as of yet.
If you were to address this in a future article I’d be grateful. The concept of allowing OAuth authentication using Facebook, Twitter, etc is something I’d love to be able to do simply in PHP.
It would seam you have heard about something before I have. I have never heard of Firebreak i will have to send out some feelers.
Hi, this is an older article. I’ve been trying for days to get this API to work and stumbled across it. I was hopeful it would finally be a solution. I followed all the steps, there were several that were new to me. Then I got to this part in the sample code (from the link “Working Version of this script can be found here”):
/************************************************
The following 3 values can be found in the setting
for the application, you created on Google
Developers console.
In order to access your GA account you must
Add the Email address as a user at the
ACCOUNT Level in the GA admin.
************************************************/
$client_id = ‘[your client]’;
$Email_address = ‘[your service account email]’;
$key_file_location = ‘[Your key]’;
I cannot find the client ID. There are several possibilities. None seem right. Also, since this is an old article, I’m not sure which library files were used. Up to that point or where it says “The Code”, this was a great tutorial. I really appreciate the effort. I got close 🙂
Try following this and then change it to calendar Google Analytics PHP service account its the best service account php tutorial that i am aware of.
I used google-api-php-client by downloading manually for release version.
So it doesn’t have library for “google-api-php-client-services”.
And only has library of “google-api-php-client”.
Is this the reason for I can’t get calendar list?
On below code, it has error if I use first line.
So I commented 1st line and used 2nd line. But it seems 2nd line can’t get list correctly.
//$calendarList = $service->calendarList->listCalendarList();
$calendarList = $calendarService->calendarList;
Service needs to be initialized properly using your client.
$service = new Google_Service_Calendar($client);
Here is the Google API Client 2.0 way to do
Go Console create Service Account and download the credential in JSON
setAuthConfig($credentials_file);
$client->setApplicationName(APP_NAME);
$client->addScope(Google_Service_Calendar::CALENDAR);
$client->addScope(Google_Service_Calendar::CALENDAR_READONLY);
//Setting Complete
//Go Google Calendar to set “Share with …” Created in Service Account (xxxxxxx@sustained-vine-198812.iam.gserviceaccount.com)
//Example of Use of API
$service = new Google_Service_Calendar($client);
$calendarId = ‘xxxx@gmail.com’; //NOT primary!! , but the email of calendar creator that you want to view
$optParams = array(
‘maxResults’ => 10,
‘orderBy’ => ‘startTime’,
‘singleEvents’ => TRUE,
‘timeMin’ => date(‘c’),
);
$results = $service->events->listEvents($calendarId, $optParams);
if (count($results->getItems()) == 0) {
print “No upcoming events found.\n”;
} else {
echo “Upcoming events:”;
echo “”;
echo “”;
foreach ($results->getItems() as $event) {
$start = $event->start->dateTime;
if (empty($start)) {
$start = $event->start->date;
}
echo “”;
echo””.$event->getSummary().””;
echo””.$start.””;
echo “”;
}
echo “”;
}
Does it actually work with using email ass the calendar id?
Hi , have you Google Hangouts ? I need talk with you ! Thanks (I’m Brazillian)
You can contact me though this form if you need help https://www.daimto.com/consulting-service/
Hi,
I have worked with this service account. I have created the service account and do all the things as document mentioned. But I got the following error message. Can you Please help me in this?
Following is my code
require_once __DIR__.’/vendor/autoload.php’;
$client_id = ‘ ? ‘; —–> what I add in this. When I create the service account I got some client id, client email address and so on.
$Email_address = ‘ ? ‘; ———-> Shall I use the email address which is generated from above.
$key_file_location = ‘TestProject.json’; ——> Here I put the downloaded file
$client = new Google_Client();
$client->setApplicationName(‘CalendarEvents’);
$key = file_get_contents($key_file_location);
$scopes =’https://www.googleapis.com/auth/calendar.readonly’;
$cred = new Google_Auth_AssertionCredentials(
$Email_address,
array($scopes),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$service = new Google_Service_Calendar($client);
$calendarId = ‘primary’;
$optParams = array(
‘maxResults’ => 10,
‘orderBy’ => ‘startTime’,
‘singleEvents’ => true,
‘timeMin’ => date(‘c’),
);
$results = $service->events->listEvents($calendarId, $optParams);
echo “”;
print_r($results);
echo “”;die;
And I got the following error “Fatal error: Uncaught Error: Class ‘Google_Auth_AssertionCredentials’ not found in your application”
try following ServiceAccount.php
Up-s! I have posted wrong place
Dear Linda, Hi!
I have about 50+ calendars wich i use in push notifications for my site
recently i had to add new one and fall i trouble
usualy i just share calendar with ?????.iam.gserviceaccount.com – and thats it, new calendar id, name etc and goes to database
now, I can’t get new calendar in the list of my calendars when i make a call .._service->calendarList->listCalendarList() in php.
OAuth works perfect , a can fetch new calendar, but i like to work with service account.
It looks like that something broken for me in google calendar when sharing for my service account
Can you give a hint please ?
Thanks in advance.
you need to have the service account insert it into calendarlist nothing is going to add it there automaticly. You may have done this before and simply forgot.
Hi Linda,
This will no longer work as Google wont allow you to add a service account email to a calendar anymore.
Would appreciate it if anyone could share a work around as I have an site that is unable to access newly created calendars.
Create a gsuite domain and then you will be able to use domain wide delegation for service accounts to domain calendar accounts.
Pingback: Google Calendar API with PHP and a Service Account won’t work anymore – Ask your php questions
You cant use service accounts with standard calendar accounts anymore you will need to use oauth2.
Hello Linda, I need to use google calendar to create events without user login. Do you have any ideas for me?
If you have a google workspace domain account you could use a service account and configure domain wide deligation.