Google 日历 API 创建组日历事件
Google calendar API create group calendar events
我的目标是通过 PHP
将活动插入群组日历
- 我已经在 api 控制台上创建了一个应用程序
- 我已激活日历 API 并创建了 API KEY
- 我已经创建了一个服务帐户as described here
这是我的代码:
class Googl_Cal{
public function __construct(){
$this->client = $this->get_client();
}
public function get_client()
{
putenv('GOOGLE_APPLICATION_CREDENTIALS=/' . dirname( __FILE__ ) . '/gcal_service_account.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$scopes = [ Google_Service_Calendar::CALENDAR ];
$client->setScopes($scopes);
return $client;
}
public function create_event(){
$service = new Google_Service_Calendar( $this->client );
$calendarList = $service->calendarList->listCalendarList();
$calendarId = '';
$event = new Google_Service_Calendar_Event(
array(
'summary' => 'Google I/O 2015',
'location' => '800 Howard St., San Francisco, CA 94103',
'description' => 'A chance to hear more about Google\'s developer products.',
'start' => array(
'dateTime' => '2018-01-31T09:00:00-07:00',
'timeZone' => 'Asia/Jerusalem',
),
'end' => array(
'dateTime' => '2018-01-31T17:00:00-07:00',
'timeZone' => 'Asia/Jerusalem',
),
'recurrence' => array(
'RRULE:FREQ=DAILY;COUNT=2'
),
'reminders' => array(
'useDefault' => FALSE,
'overrides' => array(
array('method' => 'email', 'minutes' => 24 * 60),
array('method' => 'popup', 'minutes' => 10),
),
),
)
);
$calendarId = 'primary';
$event = $service->events->insert($calendarId, $event);
print_r($event);
die();
}
}
调用create_event()后;
我在 $calendarList
上得到一个空列表
请记住,您将要插入服务帐户日历的不是您本人的服务帐户。
我怀疑你的部分问题是你没有将它插入到日历中并且可能会出错。
$calendarId = '';
我建议你这样做
$calendarId = 'primary';
这将插入到服务帐户主日历中。
假设您希望它写入您的个人日历之一。获取服务帐户电子邮件地址并在 Web google 日历应用程序中与其共享日历。然后它将能够写入它。提示 在您访问网站时从网站中获取日历 ID,因为 calendarlist.list 不会 return 它直到您添加它,这是服务帐户中的一个烦人的功能。
我的目标是通过 PHP
将活动插入群组日历- 我已经在 api 控制台上创建了一个应用程序
- 我已激活日历 API 并创建了 API KEY
- 我已经创建了一个服务帐户as described here
这是我的代码:
class Googl_Cal{
public function __construct(){
$this->client = $this->get_client();
}
public function get_client()
{
putenv('GOOGLE_APPLICATION_CREDENTIALS=/' . dirname( __FILE__ ) . '/gcal_service_account.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$scopes = [ Google_Service_Calendar::CALENDAR ];
$client->setScopes($scopes);
return $client;
}
public function create_event(){
$service = new Google_Service_Calendar( $this->client );
$calendarList = $service->calendarList->listCalendarList();
$calendarId = '';
$event = new Google_Service_Calendar_Event(
array(
'summary' => 'Google I/O 2015',
'location' => '800 Howard St., San Francisco, CA 94103',
'description' => 'A chance to hear more about Google\'s developer products.',
'start' => array(
'dateTime' => '2018-01-31T09:00:00-07:00',
'timeZone' => 'Asia/Jerusalem',
),
'end' => array(
'dateTime' => '2018-01-31T17:00:00-07:00',
'timeZone' => 'Asia/Jerusalem',
),
'recurrence' => array(
'RRULE:FREQ=DAILY;COUNT=2'
),
'reminders' => array(
'useDefault' => FALSE,
'overrides' => array(
array('method' => 'email', 'minutes' => 24 * 60),
array('method' => 'popup', 'minutes' => 10),
),
),
)
);
$calendarId = 'primary';
$event = $service->events->insert($calendarId, $event);
print_r($event);
die();
}
}
调用create_event()后; 我在 $calendarList
上得到一个空列表请记住,您将要插入服务帐户日历的不是您本人的服务帐户。
我怀疑你的部分问题是你没有将它插入到日历中并且可能会出错。
$calendarId = '';
我建议你这样做
$calendarId = 'primary';
这将插入到服务帐户主日历中。
假设您希望它写入您的个人日历之一。获取服务帐户电子邮件地址并在 Web google 日历应用程序中与其共享日历。然后它将能够写入它。提示 在您访问网站时从网站中获取日历 ID,因为 calendarlist.list 不会 return 它直到您添加它,这是服务帐户中的一个烦人的功能。