3
votes

I'm using the Google Calendar API and it is working fine but when i am trying to get all Google calendars. I am getting Blank array Although i have calendars in my google account. If try to get events of any particular calendar then it shows me all listed events of this particular event.

Here is my Code:- `

require_once __DIR__ . '/vendor/autoload.php';
// Defining basic parameters

define('APPLICATION_NAME', 'Google Sheets API PHP Quickstart');

define('CREDENTIALS_PATH', __DIR__ .'/credentials/calendar-php-quickstart.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
define('SCOPES', implode(' ', array(  Google_Service_Calendar::CALENDAR,Google_Service_Calendar::CALENDAR_READONLY)
));
putenv('GOOGLE_APPLICATION_CREDENTIALS='.CREDENTIALS_PATH);
function getClient(){
    $client = new Google_Client();
    $client->useApplicationDefaultCredentials();
    $client->addScope(Google_Service_Calendar::CALENDAR);
    $client->setApplicationName(APPLICATION_NAME);
    $client->setScopes(SCOPES);
    return $client;
}
$client = getClient();
$service = new Google_Service_Calendar($client);
$calendarList = $service->calendarList->listCalendarList();

echo "<pre>";
print_r($calendarList);
echo "</pre>"; 

Response is:-

Google_Service_Calendar_CalendarList Object
(
    [collection_key:protected] => items
    [etag] => "p33kcficlivodi0g"
    [itemsType:protected] => Google_Service_Calendar_CalendarListEntry
    [itemsDataType:protected] => array
    [kind] => calendar#calendarList
    [nextPageToken] => 
    [nextSyncToken] => COjHyZWX8NkCEkBnYW1lci1zaGVkdWxhckB0ZXN0c2VydmljZS0xNDcwNjUzMTI0Njg5LmlhbS5nc2VydmljZWFjY291bnQuY29t
    [internal_gapi_mappings:protected] => Array
        (
        )

    [modelData:protected] => Array
        (
        )

    [processed:protected] => Array
        (
        )

    [items] => Array
        (
        )

)
1
1. which type of credentials did you create? 2. listCalendarList only lists the ones that appear in the bottom left of the google calendar web view. Not necessarily all the calender's you have access to. - DaImTo
Thankx Dalm for replying. I have followed the "developers.google.com/calendar/quickstart/php" . And after that tried many codes but none of them working - Engg Php
Actually what exactly i have to do is 1. Get all the Google calendars and display them in a dropdown 2. User will select any calendar from them and add their event in this Calendar.. - Engg Php
If its returning nothing then the user has no access to no calendars which makes me think you are using a service account. At the very least normal Google users will have a primary calendar listed. Try and authenticate it again make sure you are using the correct user to login - DaImTo

1 Answers

0
votes

Try replacing:

$client->useApplicationDefaultCredentials();

With:

$client->setAuthConfig(CLIENT_SECRET_PATH); 

And add this:

$client->setAccessType('offline');