{"id":191,"date":"2016-06-30T08:35:57","date_gmt":"2016-06-30T08:35:57","guid":{"rendered":"http:\/\/symbioticindia.in\/docu\/?p=191"},"modified":"2016-06-30T08:37:31","modified_gmt":"2016-06-30T08:37:31","slug":"191","status":"publish","type":"post","link":"http:\/\/symbioticindia.in\/docu\/2016\/06\/30\/191\/","title":{"rendered":"Google Calendar with php using the google calendar api 3"},"content":{"rendered":"<h3 class=\"post-title entry-title\">Writing events to a Calendar with PHP using the Google Calendar API 3 and a Service Account (no user)<\/h3>\n<div class=\"post-header\"><\/div>\n<div id=\"post-body-7546463486056288822\" class=\"post-body entry-content\">Writing events to a Calendar with PHP using the Google Calendar API 3 and a Service Account (no user).<br \/>\n<strong><br \/>\n<\/strong><\/p>\n<h3><strong>1. Download\u00a0Google API&#8217;s PHP Client<\/strong><\/h3>\n<p>First download the Google API&#8217;s PHP Client code by following these instructions:<a href=\"https:\/\/code.google.com\/p\/google-api-php-client\/\">https:\/\/code.google.com\/p\/google-api-php-client\/<\/a>. Extract the contents of the archive and copy the resulting directory into your apps source directory (To be able to follow my example below rename the directory to &#8216;google-api-php-client&#8217; and ensure that it contains a &#8216;src&#8217; sub directory).<br \/>\n<strong><br \/>\n<\/strong><\/p>\n<h3><strong>2. Enable the Google Calendar API v3 in the API&#8217;s Console<\/strong><\/h3>\n<p>Go to <a href=\"https:\/\/cloud.google.com\/console\">https:\/\/cloud.google.com\/console<\/a>\u00a0, select the project you are working on (or create a new one) click on &#8216;APIs and Auth&#8217; and select &#8216;APIs&#8217;. Look down the list for &#8216;Calendar API&#8217; and click on the button\/switch on the right so that it turns green and says &#8216;ON&#8217;.<br \/>\n<strong><br \/>\n<\/strong><\/p>\n<h3><strong>3. Get Credentials<\/strong><\/h3>\n<p>In the cloud console (as above), click on &#8216;Registered apps&#8217;, and then &#8216;REGISTER APP&#8217;. Enter a name for your app and ensure that &#8216;Web Application&#8217; is selected next to Platform.<\/p>\n<div><strong><em><br \/>\n<\/em><\/strong><\/p>\n<h4><strong>Certificate Download<\/strong><\/h4>\n<\/div>\n<div>Expand the &#8216;Certificate&#8217; section and click on the &#8216;Generate Certificate&#8217; button. After a few seconds a &#8216;Certificate Generated&#8217; dialog will be displayed. Click on the &#8216;Download private key&#8217; button, this will trigger your browser to download a .p12 file named something like &#8216;dceffe6e95344eb64c88dd982bc8f1c01ed0aa6b-privatekey.p12&#8217;. Copy this file to a directory named &#8216;certificates&#8217; in the root of your apps source directory.<\/div>\n<div><\/div>\n<div><em><strong><br \/>\n<\/strong><\/em><\/p>\n<h4><strong>Service Account Email<\/strong><\/h4>\n<p>Click on the &#8216;View public key&#8217; button (which will dismiss the dialog). An email address will have also been populated under &#8216;EMAIL ADDRESS&#8217; in the console which will look something like:<\/p>\n<\/div>\n<div><\/div>\n<div>65436475876998-siub68asyas87d87s@developer.gserviceaccount.com<\/div>\n<div>(Make a note of this)<\/div>\n<div><strong><em><br \/>\n<\/em><\/strong><\/p>\n<h4><strong>Client ID<\/strong><\/h4>\n<p>Expand the &#8216;OAuth 2.0 Client ID&#8217; section and copy the &#8216;CLIENT ID&#8217; which will look something like:<\/p>\n<div>65436475876998-siub68asyas87d87s.apps.googleusercontent.com<\/div>\n<div>(Make a note of this)<\/div>\n<div><\/div>\n<div><strong><br \/>\n<\/strong><\/p>\n<h3><strong>5. app.yaml (or module_name.yaml) Modifications<\/strong><\/h3>\n<\/div>\n<div>\n<p>UPDATE &#8211; 20\/05\/14 &#8211; Do not include this step, it is not required.<\/p>\n<p><span style=\"text-decoration: line-through;\">To make the certificate file accessible to your code you will need to add it as a static resource, as you application will be accessing it rather than users you need to ensure you add the &#8216;application_readable&#8217;\u00a0attribute (so that your app can access it) and the login: admin attribute (so that it is not publicly accessible).<\/span><\/p>\n<\/div>\n<\/div>\n<div>\n<pre><span style=\"text-decoration: line-through;\">- url: \/certificates\/*\r\nstatic_dir: certificates\/*\r\nlogin: admin\r\napplication_readable: true<\/span><\/pre>\n<\/div>\n<div>\n<h3><strong>6. Calendar ID<\/strong><\/h3>\n<\/div>\n<div>Now you need to go and get the calendar&#8217;s ID, to do this go the calendar in Google Calendars and open its settings. On the &#8216;Calendar Details&#8217; tab next to &#8216;Calendar Address&#8217; \u00a0there is a &#8216;Calendar ID displayed, which will look something like:\u00a0sd7h90sdja97sdg9ahd0sa8bd@group.calendar.google.com<\/div>\n<div><\/div>\n<div><strong><br \/>\n<\/strong><\/p>\n<h3><strong>7. Calendar Permissions<\/strong><\/h3>\n<\/div>\n<div>Now you need to give your service account permissions on the calendar. On the &#8216;Share this Calendar&#8217; tab, under &#8216;Share with specific people&#8217;. Enter the service account email address that you got in step 3 (in this example it is\u00a065436475876998-siub68asyas87d87s@developer.gserviceaccount.com) and choose &#8216;Make changes to events&#8217;, then click Save (If you the calendar belongs to a user on a Google Apps domain you will need to ensure that permissions on the domain are setup correctly to allow external access).<\/div>\n<div><\/div>\n<div><strong><br \/>\n<\/strong><\/p>\n<h3><strong>8. Example Code<\/strong><\/h3>\n<\/div>\n<div>\n<p>Replace the details in the code example below with the details that you have gathered in the previous steps above.<\/p>\n<ul>\n<li>Lines 2 &amp; 3 &#8211; Paths to the\u00a0google-api-php-client you created earlier<\/li>\n<li>Line 8 &#8211; Client ID<\/li>\n<li>Line 11 &#8211; Service account ID<\/li>\n<li>Line 15 &#8211; Service account email<\/li>\n<li>Line 33 &#8211; Certificate file path<\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: php; ruler: true; highlight: [2, 3, 8, 11, 15, 33]\"> \/\/ ...\r\n    require_once \"..\/google-api-php-client\/src\/Google_Client.php\";\r\n    require_once \"..\/google-api-php-client\/src\/contrib\/Google_CalendarService.php\";\r\n    \/\/\r\n    $client = new Google_Client();\r\n    $client-&gt;setUseObjects(true);\r\n    $client-&gt;setApplicationName(\"your-app-name\");\r\n    $client-&gt;setClientId(\"asdohas8djasjd89absdo9p.apps.googleusercontent.com\");\r\n    $client-&gt;setAssertionCredentials(\r\n        new Google_AssertionCredentials(\r\n            \"d98hg78as9dj-0askd9ajsd08a0sdjspdj9@developer.gserviceaccount.com\",\r\n            array(\r\n                \"https:\/\/www.googleapis.com\/auth\/calendar\"\r\n            ),\r\n            file_get_contents(\"certificates\/dceffe6e95344eb64c88dd982bc8f1c01ed0aa6b-privatekey.p12\")\r\n        )\r\n    );\r\n    \/\/\r\n    $service = new Google_CalendarService($client);\r\n    \/\/\r\n    $event = new Google_Event();\r\n    $event-&gt;setSummary('Event 1');\r\n    $event-&gt;setLocation('Somewhere');\r\n    $start = new Google_EventDateTime();\r\n    $start-&gt;setDateTime('2013-10-22T19:00:00.000+01:00');\r\n    $start-&gt;setTimeZone('Europe\/London');\r\n    $event-&gt;setStart($start);\r\n    $end = new Google_EventDateTime();\r\n    $end-&gt;setDateTime('2013-10-22T19:25:00.000+01:00');\r\n    $end-&gt;setTimeZone('Europe\/London');\r\n    $event-&gt;setEnd($end);\r\n    \/\/\r\n    $calendar_id = \"sd7h90sdja97sdg9ahd0sa8bd@group.calendar.google.com\";\r\n    \/\/\r\n    $new_event = null;\r\n    \/\/\r\n    try {\r\n        $new_event = $service-&gt;events-&gt;insert($calendar_id, $event);\r\n        \/\/\r\n        $new_event_id= $new_event-&gt;getId();\r\n    } catch (Google_ServiceException $e) {\r\n        syslog(LOG_ERR, $e-&gt;getMessage());\r\n    }\r\n    \/\/\r\n    $event = $service-&gt;events-&gt;get($calendar_id, $new_event-&gt;getId());\r\n    \/\/\r\n    if ($event != null) {\r\n        echo \"Inserted:\";\r\n        echo \"EventID=\".$event-&gt;getId();\r\n        echo \"Summary=\".$event-&gt;getSummary();\r\n        echo \"Status=\".$event-&gt;getStatus();\r\n    }\r\n    \/\/...\r\n<\/pre>\n<p>This should produce output that looks like:<\/p>\n<pre>NewEventID=n5m27gad3b0mvbc8gvbe79fb7g\r\nInserted:\r\nEventID=n5m27gad3b0mvbc8gvbe79fb7g\r\nSummary=Event 1\r\nStatus=confirmed<\/pre>\n<p>See <a href=\"https:\/\/developers.google.com\/google-apps\/calendar\/\">https:\/\/developers.google.com\/google-apps\/calendar\/<\/a> for more information on the Calendar API v3.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Writing events to a Calendar with PHP using the Google Calendar API 3 and a Service Account (no user) Writing events to a Calendar with PHP using the Google Calendar API 3 and a Service Account (no user). 1. Download\u00a0Google API&#8217;s PHP Client First download the Google API&#8217;s PHP Client code by following these instructions:https:\/\/code.google.com\/p\/google-api-php-client\/. ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"http:\/\/symbioticindia.in\/docu\/2016\/06\/30\/191\/\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,6],"tags":[],"class_list":["post-191","post","type-post","status-publish","format-standard","hentry","category-google","category-php"],"_links":{"self":[{"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/posts\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/comments?post=191"}],"version-history":[{"count":3,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/posts\/191\/revisions"}],"predecessor-version":[{"id":194,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/posts\/191\/revisions\/194"}],"wp:attachment":[{"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/media?parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/categories?post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/tags?post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}