谷歌日历API无法获取日历

4

我正在使用谷歌日历API,并且它正常工作,但是当我尝试获取所有谷歌日历时,我得到了一个空数组,尽管我的谷歌帐户中有日历。如果尝试获取任何特定日历的事件,则显示此特定事件的所有列出事件。

以下是我的代码:

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. 你创建了哪种类型的凭据?
  2. listCalendarList仅列出在Google日历Web视图左下方显示的那些日历,而不一定是您可以访问的所有日历。
- Linda Lawton - DaImTo
实际上我需要做的是:
  1. 获取所有的Google日历并在下拉菜单中显示它们。
  2. 用户将从中选择任何一个日历,并将他们的事件添加到该日历中。
- Engg Php
如果返回的是空值,那么用户没有访问任何日历的权限,这让我想到您正在使用服务账户。至少普通的 Google 用户会有一个主要的日历列表。请尝试重新进行身份验证,确保您使用了正确的用户登录。 - Linda Lawton - DaImTo
Dalmto,你有任何参考资料吗? - Engg Php
1
请点击以下链接查看与Google API相关的PHP示例代码:https://github.com/LindaLawton/Google-APIs-PHP-Samples/tree/master/Samples/Calendar%20API以及使用PHP OAuth2访问Google日历的详细教程:https://www.daimto.com/accessing-google-calendar-with-php-oauth2/ - Linda Lawton - DaImTo
1个回答

0
尝试替换:
$client->useApplicationDefaultCredentials();

使用:

$client->setAuthConfig(CLIENT_SECRET_PATH); 

并添加这个:

$client->setAccessType('offline');

$calendarId = 'bh3j6b48ine1bi0ags046svjs4@group.calendar.google.com'; $optParams = array( 'maxResults' => 10, 'orderBy' => 'startTime', 'singleEvents' => TRUE, 'timeMin' => date('c'), ); $results = $service->events->listEvents($calendarId, $optParams);echo "<pre>"; print_r($results); echo "</pre>"; //echo $results->getSummary();foreach ($results->getItems() as $calendarListEntry) { echo $calendarListEntry->getSummary(); } - Engg Php
我在我的答案中添加了另一行代码,这样可以吗? - Joseph_J
在CREDENTIALS_PATH路径中,我有服务帐户JSON文件。 - Engg Php
和 CLIENT_SECRET_PATH auth ID json。 - Engg Php
我有5个日历。当我使用Google浏览器时,它会显示所有日历的列表。 - Engg Php
显示剩余27条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接