【问题标题】:No longer able to access shared calendars via Google Calendar API using service account不再能够使用服务帐户通过 Google Calendar API 访问共享日历
【发布时间】:2018-12-21 15:59:01
【问题描述】:

我有一个小 php 脚本工作了几个月,它可以通过 Google Calendar API 创建和更新 Google Calendar 事件。它工作了 2 个月,然后突然停止工作,尽管我找不到任何变化。

如果我将 calendarId 更改为“主要”(这是服务帐户拥有的唯一真正的日历),代码仍然有效。所以我知道代码本身在创建事件方面起作用,只是无法访问共享日历以在这些日历上创建事件。以下是我最初设置代码的方式以及我尝试过的方式:

1) 我创建了服务帐户并授予它“所有者”角色。然后我创建了密钥。 (请注意,这是一个 GSuite 帐户,我在其中启用了“启用 G Suite 域范围委派”)。

2) 对于我需要使用该帐户管理的每个日历,我进入了日历共享设置(“与特定人员共享”)并添加了具有“进行更改和管理共享”权限的服务帐户“电子邮件”。

3) PHP 脚本也非常基础,使用最新版本的 Google API PHP 客户端库。它进行身份验证,然后可以创建一个事件(请注意,一些细节已更改,例如我的应用名称以掩盖一些细节)。

require __DIR__ . '/vendor/autoload.php';

// Got the data and ready to authenticate
$client = new Google_Client();
$client->setApplicationName("MyAppName");
$client->setAuthConfig('credentials.json');
$client->setScopes([Google_Service_Calendar::CALENDAR_READONLY,Google_Service_Calendar::CALENDAR]);
$service = new Google_Service_Calendar($client);

// GATHER ALL PASSED IN DATA INTO VARIABLES
$calendarId = 'mycalendar@group.calendar.google.com';
$summary = "Test calendar entry";
$startDate = "2019-02-19";
$endDate = "2019-02-19";
$attendees = "user1@domain.com,user2@domain.com";

//Construct event array to send
$eventArray = array(
    'summary' => $summary,
    'start' => array(
        'date' => $startDate,
        'timeZone' => 'America/Chicago',
    ),
    'end' => array(
        'date' => $endDate,
        'timeZone' => 'America/Chicago',
    ),
    'guestsCanInviteOthers' => true,
    'guestsCanModify' => true,
    'guestsCanSeeOtherGuests' => true
);

//Only include attendees in event array if we find them
if ($data["attendees"]) {
    // Attendees are present
    $attendees = explode(",", $data["attendees"]);
    $attendeesArray = array();
    foreach ($attendees as $key => $value) {
        $attendeesArray[$key]['email'] = $value;
    };

    //Now add to original event array
    $eventArray["attendees"] = $attendeesArray;

}

//Create the event and return event id
$event = new Google_Service_Calendar_Event($eventArray);
$event = $service->events->insert($calendarId, $event);
$eventId = $event->id;
$createdDate = $event->created;

这可以进行身份​​验证,但是一旦您开始使用“主要”以外的日历,它就会出现错误:

Fatal error: Uncaught exception 'Google_Service_Exception' with message '{
    "error": {
    "errors": [
    {
    "domain": "global",
    "reason": "notFound",
    "message": "Not Found"
    }
    ],
    "code": 404,
    "message": "Not Found"
    }
}

我通过谷歌搜索,查看了该网站上的许多教程和建议,我尝试过的所有操作,包括完全重置服务帐户都没有解决问题。

【问题讨论】:

    标签: php google-calendar-api


    【解决方案1】:

    我不确定 Google 是否更改了它的工作方式,或者我的 IT 是否弄乱了设置,因为我知道这已经工作了一段时间。不过,我确实找到了解决办法!

    必须更改另一个设置,该设置只能由 GSuite 管理员设置。步骤如下:

    1. 在您的 Google 管理控制台中(位于 admin.google.com)...
    2. 转到应用程序 > G Suite > 日历。 (这需要拥有日历 > 设置管理员权限。)
    3. 点击共享设置。
    4. 在“外部共享选项”部分中,选择“共享所有信息,并且外部人员可以更改日历”
    5. 如果我没记错的话,我什至不需要点击保存,设置是即时的(不管消息说可能需要一段时间)。

    在这里找到完整的详细信息 - https://support.google.com/a/answer/60765?hl=en

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-27
      • 2017-06-26
      • 1970-01-01
      • 1970-01-01
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      相关资源
      最近更新 更多