未经用户授权访问 API 中的 GSuite 用户日历

Access GSuite users Calendars from API without authorization from users

我正在努力 API 访问和管理 (CRUD) 属于我的 GSuite 帐户的所有用户日历。 我想简单地访问用户数据而不需要他们的授权。

遵循所有相关指南后,
1) 设置服务帐户 https://developers.google.com/calendar/auth
2) 将全域权限委托给帐户https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
3) 将 auth creds 下载为 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 日历 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 link 时,它会将我带到日历并显示错误消息“无法找到请求的事件..

感谢任何帮助!!! 谢谢

我缺少 setSubject() 方法。
这会设置您要模拟的用户。
因此,当我调用 $this-api_client->setSubject('user@example.com'); 然后在使用 'primary' 关键字插入时,它成功将该事件插入到用户@example.com 的日历中,而不需要他们的授权!!!
布里尔!