未经用户授权,从API访问GSuite用户日历

问题描述 投票:0回答:1

我正在努力使用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' => '[email protected]'),
                array('email' => '[email protected]'),
            ),
            '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调用snapshot of google calendar api metrics

但我找不到在日历中任何地方插入的事件!这是数组的输出

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] => [email protected]
    [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] => [email protected]
            [id] => 
            [self] => 1
            [internal_gapi_mappings:protected] => Array
                (
                )

            [modelData:protected] => Array
                (
                )

            [processed:protected] => Array
                (
                )

        )

    [organizer] => Google_Service_Calendar_EventOrganizer Object
        (
            [displayName] => 
            [email] => [email protected]
            [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] => [email protected]
                    [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] => [email protected]
                    [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链接时,它会将我带到日历并显示无法找到请求事件的错误消息。

任何帮助表示赞赏!谢谢

php google-api google-calendar-api google-oauth2 service-accounts
1个回答
0
投票

我错过了setSubject()方法。 这将设置您要模拟的用户。 因此,当我调用$this-api_client->setSubject('[email protected]');然后在插入时使用'primary'关键字时,它成功地将该事件插入到[email protected]的日历中,而无需他们的授权! 布里尔!

© www.soinside.com 2019 - 2024. All rights reserved.