php - How can I show a list of *all* available calendars using Google Calendar API v3 / Google API Client Library? -


i have been trying access google calendar api v3 using php. initially, want list user calendars accessible call api.

to so, have downloaded google api php client library , have attempted use following code (which sourced, adaptations, https://mytechscraps.wordpress.com/2014/05/15/accessing-google-calendar-using-the-php-api/ ):

<?php  //error_reporting(0); //@ini_set('display_errors', 0);  // if you've used composer include library, remove following line // , make sure follow standard composer autoloading. // https://getcomposer.org/doc/01-basic-usage.md#autoloading require_once './google-api-php-client-master/autoload.php';  // service account info $client_id = '754612121864-pmdfssdfakqiqblg6lt9a.apps.googleusercontent.com'; $service_account_name = '754674507864-pm1dsgdsgsdfsdfsdfdflg6lt9a@developer.gserviceaccount.com'; $key_file_location = 'calendar-7dsfsdgsdfsda953d68dgdsgdsff88a.p12';   $client = new google_client(); $client->setapplicationname("calendar");  $service = new google_service_calendar($client);  $key = file_get_contents($key_file_location); $cred = new google_auth_assertioncredentials(  $service_account_name,  array('https://www.googleapis.com/auth/calendar.readonly'),  $key );  $client->setassertioncredentials($cred);  $cals = $service->calendarlist->listcalendarlist(); print_r($cals);  ?> 

i have created service account in google developers console, , generated oauth details, have used set appropriate variables can seen code.

this code returns following:

google_service_calendar_calendarlist object ( [collection_key:protected] => items [internal_gapi_mappings:protected] => array ( ) [etag] => "14171313334000" [itemstype:protected] => google_service_calendar_calendarlistentry [itemsdatatype:protected] => array [kind] => calendar#calendarlist [nextpagetoken] => [nextsynctoken] => 000014121268327000 [modeldata:protected] => array ( [items] => array ( [0] => array ( [kind] => calendar#calendarlistentry [etag] => "1417721316542000" [id] => bots@madeupcompany.co.uk [summary] => bots@madeupcompany.co.uk [timezone] => europe/london [colorid] => 23 [backgroundcolor] => #cd74e6 [foregroundcolor] => #000000 [selected] => 1 [accessrole] => reader [defaultreminders] => array ( ) ) ) ) [processed:protected] => array ( ) ) 

the problem seems returning details of one calendar. is, calendar bots@madeupcompany.co.uk (the 1 explicitly shared service).

however, know google account has read-only access number of other user's calendars (i can see them when logged google calendar user).

furthermore, if use google apps explorer on page: https://developers.google.com/google-apps/calendar/v3/reference/calendarlist/list#auth , while logged google account bots@madeupcompany.co.uk, details of all of these other calendars.

so i'm trying work out, why apps explorer showing me other calendars, , yet php code not?

a service account doesn’t need prompt user access because have set up. go google calendar website. find calendar settings , go calendars tab, find calendar want access , click on “shared: edit settings” add service account email address persons email address. give service account same access if sharing other user.

<?php session_start();         require_once 'google/client.php'; require_once 'google/service/calendar.php';      /************************************************     following 3 values befound in setting     application created on google        developers console.         developers console.  key file should placed in location       not accessable web. outside of   web root.      in order access ga account must      add email address user @       account level in ga admin.           ************************************************/ $client_id = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp.apps.googleusercontent.com'; $email_address = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp@developer.gserviceaccount.com';      $key_file_location = '629751513db09cd21a941399389f33e5abd633c9-privatekey.p12';      $client = new google_client();       $client->setapplicationname("client_library_examples"); $key = file_get_contents($key_file_location);     // seproate additional scopes 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);      ?>  <html><body>  <?php $calendarlist  = $service->calendarlist->listcalendarlist(); print_r($calendarlist); while(true) {     foreach ($calendarlist->getitems() $calendarlistentry) {         echo "<a href='oauth2.php?type=event&id=".$calendarlistentry->id." '>".$calendarlistentry->getsummary()."</a><br>\n";     }     $pagetoken = $calendarlist->getnextpagetoken();     if ($pagetoken) {         $optparams = array('pagetoken' => $pagetoken);         $calendarlist = $service->calendarlist->listcalendarlist($optparams);     } else {         break;     } }      ?> </html>  

code ripped tutorial google calendar api php – service account


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -