{"id":1637,"date":"2016-04-18T09:50:13","date_gmt":"2016-04-18T07:50:13","guid":{"rendered":"https:\/\/blog.kodono.info\/wordpress\/?p=1637"},"modified":"2016-08-20T12:13:32","modified_gmt":"2016-08-20T10:13:32","slug":"get-color_id-from-a-google-calendar-using-api-php","status":"publish","type":"post","link":"https:\/\/blog.kodono.info\/wordpress\/2016\/04\/18\/get-color_id-from-a-google-calendar-using-api-php\/","title":{"rendered":"Get color_id from a Google Calendar using API [PHP]"},"content":{"rendered":"<p>For a project I needed to get the events from a Google Calendar, as well as the colors from it.<\/p>\n<p>It&#8217;s been a pain, but after a couple of days I&#8217;ve been able to create a PHP page to do so. It will use the <a href=\"http:\/\/www.riskcompletefailure.com\/2015\/03\/understanding-service-accounts.html\">server-to-server auth mechanism<\/a>.<\/p>\n<p>I&#8217;ll try to provide the different steps (please note that my Google Console is in French so I tried to translate) :<\/p>\n<ol>\n<li>Go to <a href=\"https:\/\/console.developers.google.com\/apis\/api\/calendar\/overview\">https:\/\/console.developers.google.com\/apis\/api\/calendar\/overview<\/a> and <strong>Activate the Calendar API<\/strong> (or create first a project if required)<\/li>\n<li>Once the API is activated you should be invited to go to the Identification part ; make sure you choose <strong>&#8220;Account Service Key&#8221;<\/strong> for the identification mode<\/li>\n<li>Then pick <strong>&#8220;New Service&#8221;<\/strong> and complete the fields (choose JSON for the file format)<\/li>\n<li>Download the JSON file and save it where your PHP file will stand on your web server<\/li>\n<li>Open the JSON file and search for the <strong>client_email<\/strong><\/li>\n<li>Go to <a href=\"https:\/\/calendar.google.com\/\">https:\/\/calendar.google.com\/<\/a> and in the sharing parameters : make sure that you share your calendar in editing with the <strong>client_email<\/strong> address found in the previous step (the editing mode is required to get the Colors)<\/li>\n<li>Now you need to install the <a href=\"https:\/\/developers.google.com\/api-client-library\/php\/start\/installation\">PHP library from Google<\/a> &#8230; in my case I don&#8217;t have a console access on the server, so I transfered the files from Github to the web server, in the same directory as our PHP file will be (so I got a folder called <em>google-api-php-client-1.1.7<\/em>)<\/li>\n<li>Create your PHP file with the below content :\n<pre class=\"brush:php\">\r\n&lt;?php\r\n\/\/ make sure the include path got the Google API (please refer to https:\/\/developers.google.com\/api-client-library\/php\/start\/installation)\r\nset_include_path(get_include_path() . PATH_SEPARATOR . '\/full\/path\/WordPress\/wp-content\/agenda\/google-api-php-client-1.1.7\/src\/Google');\r\nrequire_once \"autoload.php\";\r\n\r\n\/\/ Connect to the Google Calendar Service\r\n$client = new Google_Client();\r\n$client->setApplicationName(\"Google Calendar\");\r\n$data = json_decode(file_get_contents('YourKeyFile-xxxxx.json'));\r\n$client_id = $data->client_id;\r\n$client_email = $data->client_email;\r\n$cred = new Google_Auth_AssertionCredentials(\r\n  $client_email,\r\n  array(Google_Service_Calendar::CALENDAR),\r\n  $data->private_key\r\n);\r\n\r\n\/\/ Remember to cache the access token locally if making multiple calls\r\n\/\/ and don't just use this function for each request!\r\n$client->getAuth()->refreshTokenWithAssertion($cred);\r\n$service = new Google_Service_Calendar($client);\r\n$optParams = array(\r\n  \"calendarId\"   => \"TheGoogleAccountForTheCalendar@gmail.com\",\r\n  \"singleEvents\" => true,\r\n  \"timeZone\"     => \"Europe\/Paris\",\r\n  \"maxResults\"   => 250,\r\n  \"timeMin\"      => date(\"c\", strtotime(\"midnight\")), \/* to get events from today... *\/\r\n  \"timeMax\"      => date(\"c\",strtotime('+3 day')), \/* ...to the 3 next days *\/\r\n  \"orderBy\"      => \"startTime\"\r\n);\r\n$events = $service->events->listEvents('TheGoogleAccountForTheCalendar@gmail.com', $optParams);\r\n?>\r\n&lt;!DOCTYPE html>\r\n&lt;html>\r\n  &lt;head>\r\n    &lt;meta charset=\"UTF-8\">\r\n    &lt;meta http-equiv=\"Content-Language\" content=\"fr\" \/>\r\n    &lt;meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge,chrome=1\" \/>\r\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1\">\r\n  &lt;\/head>\r\n  &lt;body>\r\n&lt;?php\r\n  while(true) {\r\n    foreach ($events->getItems() as $event) {\r\n      $endTime = new DateTime($event->getEnd()->getDateTime());\r\n      echo $event->getSummary() .\" [\".$event->getColorId().\"] (\".$endTime->format('Y-m-d').\")&lt;br>\";\r\n    }\r\n    $pageToken = $events->getNextPageToken();\r\n    if ($pageToken) {\r\n      $optParams['pageToken'] = $pageToken;\r\n      $events = $service->events->listEvents('lgpmontpellier@gmail.com', $optParams);\r\n    } else {\r\n      break;\r\n    }\r\n  }\r\n?>\r\n  &lt;\/body>\r\n&lt;\/html>\r\n<\/pre>\n<p>Finally you can refer to the <a href=\"https:\/\/developers.google.com\/google-apps\/calendar\/?csw=1\">Google Calendar API<\/a>.<\/p>\n<p>FYI I retrieved the below colorId for the below available colors:<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.kodono.info\/wordpress\/wp-content\/uploads\/2016\/04\/colors_google_calendar.png\" alt=\"\" width=\"288\" height=\"41\" class=\"aligncenter size-full wp-image-1685\" \/><\/p>\n<pre class=\"brush:php\">\r\n    \/\/ same order as the image above\r\n    switch ($event->getColorId()) {\r\n      case 9: $color=\"#5484ED\"; break;\r\n      case 1: $color=\"#A4BDFC\"; break;\r\n      case 7: $color=\"#46D6DB\"; break;\r\n      case 2: $color=\"#7AE7BF\"; break;\r\n      case 10: $color=\"#51B749\"; break;\r\n      case 5: $color=\"#FBD75B\"; break;\r\n      case 6: $color=\"#FFB878\"; break;\r\n      case 4: $color=\"#FF887C\"; break;\r\n      case 11: $color=\"#DC2127\"; break;\r\n      case 3: $color=\"#DBADFF\"; break;\r\n      case 8: $color=\"#E1E1E1\"; break;\r\n      default: $color=\"#AC725E\"; \/\/ the first one in the picture, the one that is checked\r\n    }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>For a project I needed to get the events from a Google Calendar, as well as the colors from it. It&#8217;s been a pain, but after a couple of days I&#8217;ve been able to create a PHP page to do so. It will use the server-to-server auth mechanism. I&#8217;ll try to provide the different steps [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","hide_page_title":"","footnotes":""},"categories":[15,170,13,33,26],"tags":[153,123,152,25,158],"class_list":["post-1637","post","type-post","status-publish","format-standard","hentry","category-astuce","category-english","category-niveau-intermediaire","category-programmation","category-web-design","tag-astuce","tag-english","tag-niveau-intermediaire","tag-php","tag-programmation"],"_links":{"self":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/comments?post=1637"}],"version-history":[{"count":9,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1637\/revisions"}],"predecessor-version":[{"id":1686,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/1637\/revisions\/1686"}],"wp:attachment":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/media?parent=1637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/categories?post=1637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/tags?post=1637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}