我正努力使用API访问和管理(CRUD)属于我的GSuite帐户的所有用户日历。 我只想访问用户数据而无需他们的授权。
遵循所有相关指南后,
1)设置服务帐户https://developers.google.com/calendar/auth
2)将域范围的权限委派给帐户https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
3)将身份验证凭据下载为json文件,然后调用$this->api_client->setAuthConfig('creds.json');
代码如下:
protected $api_client;
protected $service;
public function __construct()
{
$this->api_client = new Google_Client();
$this->api_client->setApplicationName("XXXX");
$this->api_client->setScopes([
Google_Service_Directory::ADMIN_DIRECTORY_USER,
Google_Service_Directory::ADMIN_DIRECTORY_GROUP,
Google_Service_Directory::ADMIN_DIRECTORY_USER_ALIAS,
Google_Service_Directory::ADMIN_DIRECTORY_GROUP_MEMBER,
Google_Service_Calendar::CALENDAR
]);
$this->setAuthentication();
$this->service = new Google_Service_Directory($this->api_client);
}
private function setAuthentication()
{
$this->api_client->setAuthConfig('creds.json');
$this->api_client->setAccessType("offline");
}
public function createEvent()
{
$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' => '2019-04-15T09:00:00-07:00',
'timeZone' => 'America/Los_Angeles',
),
'end' => array(
'dateTime' => '2019-04-15T17:00:00-07:00',
'timeZone' => 'America/Los_Angeles',
),
'recurrence' => array(
'RRULE:FREQ=DAILY;COUNT=2'
),
'attendees' => array(
array('email' => 'lpage@example.com'),
array('email' => 'sbrin@example.com'),
),
'reminders' => array(
'useDefault' => FALSE,
'overrides' => array(
array('method' => 'email', 'minutes' => 24 * 60),
array('method' => 'popup', 'minutes' => 10),
),
),
));
$service = new Google_Service_Calendar($this->api_client);
$new_event = $service->events->insert('primary', $event);
return $new_event;
}
$g = new GSuiteAdmin();
$new = $g->createEvent();
echo "<pre>";
print_r($new);
我得到一个没有任何错误的数组。另外,这是Google Calendar API指标的屏幕快照,似乎表明在进行API调用时没有出现任何错误。
但是我找不到日历中任何地方插入的活动! 这是数组的输出
Google_Service_Calendar_Event Object
(
[collection_key:protected] => recurrence
[anyoneCanAddSelf] =>
[attachmentsType:protected] => Google_Service_Calendar_EventAttachment
[attachmentsDataType:protected] => array
[attendeesType:protected] => Google_Service_Calendar_EventAttendee
[attendeesDataType:protected] => array
[attendeesOmitted] =>
[colorId] =>
[conferenceDataType:protected] => Google_Service_Calendar_ConferenceData
[conferenceDataDataType:protected] =>
[created] => 2019-04-15T08:49:21.000Z
[creatorType:protected] => Google_Service_Calendar_EventCreator
[creatorDataType:protected] =>
[description] => A chance to hear more about Google\'s developer products.
[endType:protected] => Google_Service_Calendar_EventDateTime
[endDataType:protected] =>
[endTimeUnspecified] =>
[etag] => "3110636323283000"
[extendedPropertiesType:protected] => Google_Service_Calendar_EventExtendedProperties
[extendedPropertiesDataType:protected] =>
[gadgetType:protected] => Google_Service_Calendar_EventGadget
[gadgetDataType:protected] =>
[guestsCanInviteOthers] =>
[guestsCanModify] =>
[guestsCanSeeOtherGuests] =>
[hangoutLink] =>
[htmlLink] => https://www.google.com/calendar/event?eid=bW1sZWV1NTlvXXXo1NWhuMGpxaXI1NXNxazBfMjAxOTA0MTVUMTYwMDAwWiBjYWxlbmRhcmFwaUBjYWxlbmRhcmFwaS0yMzc2MTAuaWFtLmdzZXJ2aWNlYWNjb3VudC5jb20
[iCalUID] => mmleeu59osj55hn0jqir55sqk0@google.com
[id] => mmleeu59osj55hn0jqir55sqk0
[kind] => calendar#event
[location] => 800 Howard St., San Francisco, CA 94103
[locked] =>
[organizerType:protected] => Google_Service_Calendar_EventOrganizer
[organizerDataType:protected] =>
[originalStartTimeType:protected] => Google_Service_Calendar_EventDateTime
[originalStartTimeDataType:protected] =>
[privateCopy] =>
[recurrence] => Array
(
[0] => RRULE:FREQ=DAILY;COUNT=2
)
[recurringEventId] =>
[remindersType:protected] => Google_Service_Calendar_EventReminders
[remindersDataType:protected] =>
[sequence] => 0
[sourceType:protected] => Google_Service_Calendar_EventSource
[sourceDataType:protected] =>
[startType:protected] => Google_Service_Calendar_EventDateTime
[startDataType:protected] =>
[status] => confirmed
[summary] => Google I/O 2015
[transparency] =>
[updated] => 2019-04-15T08:49:21.683Z
[visibility] =>
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
[creator] => Google_Service_Calendar_EventCreator Object
(
[displayName] =>
[email] => calendarapi@calendarapi-237610.iam.gserviceaccount.com
[id] =>
[self] => 1
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
[organizer] => Google_Service_Calendar_EventOrganizer Object
(
[displayName] =>
[email] => calendarapi@calendarapi-237610.iam.gserviceaccount.com
[id] =>
[self] => 1
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
[start] => Google_Service_Calendar_EventDateTime Object
(
[date] =>
[dateTime] => 2019-04-15T16:00:00Z
[timeZone] => America/Los_Angeles
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
[end] => Google_Service_Calendar_EventDateTime Object
(
[date] =>
[dateTime] => 2019-04-16T00:00:00Z
[timeZone] => America/Los_Angeles
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
[attendees] => Array
(
[0] => Google_Service_Calendar_EventAttendee Object
(
[additionalGuests] =>
[comment] =>
[displayName] =>
[email] => sbrin@example.com
[id] =>
[optional] =>
[organizer] =>
[resource] =>
[responseStatus] => needsAction
[self] =>
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
[1] => Google_Service_Calendar_EventAttendee Object
(
[additionalGuests] =>
[comment] =>
[displayName] =>
[email] => lpage@example.com
[id] =>
[optional] =>
[organizer] =>
[resource] =>
[responseStatus] => needsAction
[self] =>
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
)
[reminders] => Google_Service_Calendar_EventReminders Object
(
[collection_key:protected] => overrides
[overridesType:protected] => Google_Service_Calendar_EventReminder
[overridesDataType:protected] => array
[useDefault] =>
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
[overrides] => Array
(
[0] => Google_Service_Calendar_EventReminder Object
(
[method] => popup
[minutes] => 10
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
[1] => Google_Service_Calendar_EventReminder Object
(
[method] => email
[minutes] => 1440
[internal_gapi_mappings:protected] => Array
(
)
[modelData:protected] => Array
(
)
[processed:protected] => Array
(
)
)
)
)
)
当我转到提供的HTML链接时,它将带我进入日历并显示错误消息“找不到请求的事件。”
任何帮助表示赞赏!!! 谢谢
答案 0 :(得分:0)
我缺少setSubject()方法。
设置要模拟的用户。
因此,当我调用inputType: 'password'
时,然后在插入时使用'primary'关键字即可将事件成功插入到user@example.com的日历中,而无需其授权!
太棒了!