Google Calendar API无法获取日历

时间:2018-03-16 06:26:41

标签: php google-calendar-api

我正在使用Google Calendar API,但它正常运行,但是当我想要获取所有Google日历时。我正在获取空白阵列虽然我的谷歌帐户中有日历。如果尝试获取任何特定日历的事件,则会向我显示此特定事件的所有列出的事件。

这是我的代码: - `

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>"; 

回应是: -

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 个答案:

答案 0 :(得分:0)

尝试更换:

$client->useApplicationDefaultCredentials();

使用:

$client->setAuthConfig(CLIENT_SECRET_PATH); 

并添加:

$client->setAccessType('offline');