Google Calendar API v3 - 不创建事件(服务器到服务器身份验证)

时间:2014-03-28 18:19:12

标签: php api google-api google-oauth google-api-php-client

因此,经过几个小时的挖掘,我无法解决问题。我正在尝试使用Google的Calendar API v3在我的主日历上创建一个活动,但我没有遇到预期的行为。

我在此实例中使用服务器到服务器身份验证。为此,我创建了服务帐户凭据,将我的密钥对移动到与我的测试脚本相同的目录,并在开发者控制台中将Google Calendar API设置为“On”。

您将在下面找到有争议的代码。

预期结果(如Google所述)是在成功创建/添加到日历的事件后,将返回Event对象或数组(根据您的需要)。在任何一种情况下,我都会收到Event对象或数组,但事件不会显示在日历中。如果我尝试访问随创建的事件数据集提供的“htmlLink”,我会收到一条错误消息,指出“此事件不存在。”

我想,因为我收到了一个带有所有必要细节的Event对象,所以它成功了;但没有雪茄。我尝试使用事件的“可见性”属性(将其设置为“公共”),但仍然没有运气。

如果有人能帮助我,我真的很感激。

[代码]

require_once __DIR__ . '/GoogleAPI/Google_Client.php';
require_once __DIR__ . '/GoogleAPI/contrib/Google_CalendarService.php';

$google = new Google_Client();

$google->setUseObjects(TRUE);
$google->setClientId(%my_client_id%);
$google->setAssertionCredentials(
    new Google_AssertionCredentials(
        %my_client_id_email%,
        array(
            'https://www.googleapis.com/auth/calendar',
            'https://www.googleapis.com/auth/calendar.readonly'
        ),
        file_get_contents(__DIR__ . '/google_key.p12')
    )
);

$service = new Google_CalendarService($google);

$event = new Google_Event();

$event->setSummary('Test Appointment');
$event->setLocation('Testing');
$event->setVisibility('public');

$start = new Google_EventDateTime();
$start->setDateTime('2014-03-28T10:00:00.000-05:00');
$event->setStart($start);

$end = new Google_EventDateTime();
$end->setDateTime('2014-03-28T10:30:00.000-05:00');
$event->setEnd($end);

$createdEvent = $service->events->insert('primary', $event, array('maxAttendees' => 1, 'sendNotifications' => FALSE));

var_dump($createdEvent);

=============================================== ==============================

[结果]

object(Google_Event)#20 (56) {
  ["anyoneCanAddSelf"]=>
  NULL
  ["__attendeesType":protected]=>
  string(20) "Google_EventAttendee"
  ["__attendeesDataType":protected]=>
  string(5) "array"
  ["attendees"]=>
  NULL
  ["attendeesOmitted"]=>
  NULL
  ["colorId"]=>
  NULL
  ["created"]=>
  string(24) "2014-03-28T18:14:59.000Z"
  ["__creatorType":protected]=>
  string(19) "Google_EventCreator"
  ["__creatorDataType":protected]=>
  string(0) ""
  ["creator"]=>
  object(Google_EventCreator)#21 (4) {
    ["displayName"]=>
    NULL
    ["email"]=>
    string(75) "%my_client_id_email%"
    ["id"]=>
    NULL
    ["self"]=>
    bool(true)
  }
  ["description"]=>
  NULL
  ["__endType":protected]=>
  string(20) "Google_EventDateTime"
  ["__endDataType":protected]=>
  string(0) ""
  ["end"]=>
  object(Google_EventDateTime)#24 (3) {
    ["date"]=>
    NULL
    ["dateTime"]=>
    string(20) "2014-03-28T15:30:00Z"
    ["timeZone"]=>
    NULL
  }
  ["endTimeUnspecified"]=>
  NULL
  ["etag"]=>
  string(52) ""2DaeHpkENZGECFHdcr5l8tYxjD4/MTM5NjAzMDQ5OTI1MTAwMA""
  ["__extendedPropertiesType":protected]=>
  string(30) "Google_EventExtendedProperties"
  ["__extendedPropertiesDataType":protected]=>
  string(0) ""
  ["extendedProperties"]=>
  NULL
  ["__gadgetType":protected]=>
  string(18) "Google_EventGadget"
  ["__gadgetDataType":protected]=>
  string(0) ""
  ["gadget"]=>
  NULL
  ["guestsCanInviteOthers"]=>
  NULL
  ["guestsCanModify"]=>
  NULL
  ["guestsCanSeeOtherGuests"]=>
  NULL
  ["hangoutLink"]=>
  NULL
  ["htmlLink"]=>
  string(178) "https://www.google.com/calendar/event?eid=ZXNrYjdzNHBvZzlidnVhYjdqbmY2bWI3aWMgNjI1NDE0OTkwNTU3LW10cmpwaDZzNzRyNTVrbmFpc2p0OHVhY3ZqY3IxYzRxQGRldmVsb3Blci5nc2VydmljZWFjY291bnQuY29t"
  ["iCalUID"]=>
  string(37) "eskb7s4pog9bvuab7jnf6mb7ic@google.com"
  ["id"]=>
  string(26) "eskb7s4pog9bvuab7jnf6mb7ic"
  ["kind"]=>
  string(14) "calendar#event"
  ["location"]=>
  string(7) "Testing"
  ["locked"]=>
  NULL
  ["__organizerType":protected]=>
  string(21) "Google_EventOrganizer"
  ["__organizerDataType":protected]=>
  string(0) ""
  ["organizer"]=>
  object(Google_EventOrganizer)#22 (4) {
    ["displayName"]=>
    NULL
    ["email"]=>
    string(75) "%my_client_id_email%"
    ["id"]=>
    NULL
    ["self"]=>
    bool(true)
  }
  ["__originalStartTimeType":protected]=>
  string(20) "Google_EventDateTime"
  ["__originalStartTimeDataType":protected]=>
  string(0) ""
  ["originalStartTime"]=>
  NULL
  ["privateCopy"]=>
  NULL
  ["recurrence"]=>
  NULL
  ["recurringEventId"]=>
  NULL
  ["__remindersType":protected]=>
  string(21) "Google_EventReminders"
  ["__remindersDataType":protected]=>
  string(0) ""
  ["reminders"]=>
  object(Google_EventReminders)#25 (4) {
    ["__overridesType":protected]=>
    string(20) "Google_EventReminder"
    ["__overridesDataType":protected]=>
    string(5) "array"
    ["overrides"]=>
    NULL
    ["useDefault"]=>
    bool(true)
  }
  ["sequence"]=>
  int(0)
  ["__sourceType":protected]=>
  string(18) "Google_EventSource"
  ["__sourceDataType":protected]=>
  string(0) ""
  ["source"]=>
  NULL
  ["__startType":protected]=>
  string(20) "Google_EventDateTime"
  ["__startDataType":protected]=>
  string(0) ""
  ["start"]=>
  object(Google_EventDateTime)#23 (3) {
    ["date"]=>
    NULL
    ["dateTime"]=>
    string(20) "2014-03-28T15:00:00Z"
    ["timeZone"]=>
    NULL
  }
  ["status"]=>
  string(9) "confirmed"
  ["summary"]=>
  string(16) "Test Appointment"
  ["transparency"]=>
  NULL
  ["updated"]=>
  string(24) "2014-03-28T18:14:59.251Z"
  ["visibility"]=>
  string(6) "public"
}

1 个答案:

答案 0 :(得分:2)

它在服务帐户拥有的日历中创建活动,如果您想管理它,您必须与自己共享日历(!):

$scope = new Google_AclRuleScope();
$scope->setType('user');
$scope->setValue('YOUR-EMAIL-HERE');

$rule = new Google_AclRule();
$rule->setRole('owner');
$rule->setScope($scope);

$result = $service->acl->insert('primary', $rule);

参考:Who owns calendars created by service account via Google Calendar API and how can they be shared?

PS:有一个新版本的PHP客户端库。