我的目标是将事件插入到VIA PHP
组日历中这是我的代码:
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上得到一个空列表
答案 0 :(得分:1)
请记住,服务帐户不是您将要插入服务帐户日历的。
我怀疑您的部分问题是您没有将其插入日历中,并且可能会收到错误。
$ calendarId ='';
我认为你做了
$ calendarId ='primary';
这将插入服务帐户主日历。
假设您希望将其写入您的某个个人日历。获取服务帐户电子邮件地址,并在Web Google日历应用中与其共享日历。然后它就可以写入它。提示在您访问网站时抓取日历ID,因为calendarlist.list不会返回它,直到您添加它,这是服务帐户中的烦人功能。