Engg Php
Engg Php

Reputation: 61

Google Calendar API can't get calendars

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
        (
        )

)

Upvotes: 4

Views: 676

Answers (1)

Joseph_J
Joseph_J

Reputation: 3669

Try replacing:

$client->useApplicationDefaultCredentials();

With:

$client->setAuthConfig(CLIENT_SECRET_PATH); 

And add this:

$client->setAccessType('offline');

Upvotes: 0

Related Questions