Google 日历 api 用户使用 php 定义的 ID

Google calendar api user defined id using php

我正在创建数百个日历,而不是分配用户。我需要定期更新日历,因此我需要为每个日历创建自定义 ID。无论我使用什么,它都会返回无效 ID

下面是我在 PHP 中的脚本

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


define('APPLICATION_NAME', 'Assignments Calendar');
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/Assignment_Calendars-701dce094981.json');
define('SCOPES', implode(' ', array(
        Google_Service_Calendar::CALENDAR)
));

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(SCOPES);
$calendarServiceAccount = new Google_Service_Calendar($client);
 $calendarId="abcdefghijklmnopqrstuvwxyz1234567890";
$newCalendar = new Google_Service_Calendar_Calendar();
$newCalendar->setSummary("testing");
$newCalendar->setTimeZone('America/Chicago');
$newCalendar->setId($calendarId);
$calendar = $calendarServiceAccount->calendars->insert($newCalendar);

我的错误信息是

PHP Fatal error:  Uncaught Google_Service_Exception: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalid",
    "message": "Invalid resource id value."
   }
  ],
  "code": 400,
  "message": "Invalid resource id value."
 }
}

无论我如何更改 $calendarId 都会得到同样的错误。如果我删除 $newCalendar->setId($calendarId);它完美运行

您不能分配自定义 ID,日历的 ID 由 API 定义。参见 API reference

您必须创建日历,在 insert 调用后读取 $calendar->id 值并将其存储在某处以 link 以任何方式提供给用户。