【问题标题】:Google Calendar API v3 - Not Creating Event (Server-to-Server Authentication)Google Calendar API v3 - 不创建事件(服务器到服务器身份验证)
【发布时间】:2014-03-28 18:19:12
【问题描述】:

所以在挖掘了几个小时之后,我不知所措。我正在尝试使用 Google 日历 API v3 在我的主日历上创建一个活动,但我没有遇到预期的行为。

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

您将在下面找到相关代码。

预期的结果(正如 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"
}

【问题讨论】:

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


    【解决方案1】:

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

    $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 客户端库。

    【讨论】:

    • 非常感谢!出于某种原因,我的印象是服务帐户将使用“帐户”主日历,但这使一切都清楚,知道服务帐户本质上是一个具有自己的日历/日历系统的虚拟帐户,并且需要如此对待。拉出一个漂亮的头发与这个战斗,非常感谢你:) 关于 PHP 客户端版本,确实我现在已经切换回最新版本。谢谢。
    猜你喜欢
    • 2019-07-23
    • 2012-04-09
    • 2020-03-20
    • 2020-04-22
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 2022-11-30
    • 2016-01-02
    相关资源
    最近更新 更多