Google Calendar API v3 - 不创建事件(服务器到服务器身份验证) [英] Google Calendar API v3 - Not Creating Event (Server-to-Server Authentication)

查看:33
本文介绍了Google Calendar API v3 - 不创建事件(服务器到服务器身份验证)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以在挖掘了几个小时之后,我对解决方案感到茫然.我正在尝试使用 Google 日历 API 的 v3 在我的主日历上创建一个事件,但我没有遇到预期的行为.

So after digging around for several hours, I'm at a loss for a solution. I'm attempting to create an event on my primary calendar using v3 of Google's Calendar API, but I'm not experiencing the expected behavior.

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

I'm using server-to-server authentication in this instance. To do that I created service account credentials, moved my key pair to the same directory as my test script, and set Google Calendar API to "On" within the developer console.

您会在下方找到有问题的代码.

Below you'll find the code at issue.

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

The expected result (as mentioned by Google) is that upon an event being successfully created/added to a calendar, an Event object or array (to your desire) will be returned. In either case I receive the Event object or array, but the event doesn't show up in the calendar. If I try to visit the "htmlLink" that's supplied with the created event data set, I receive an error message that says "This event does not exist."

我想,既然我得到了一个包含所有必要细节的 Event 对象,它就成功了;但没有雪茄.我尝试使用事件的可见性"属性(将其设置为公开"),但仍然没有运气.

I'd imagine that since I'm getting an Event object back with all necessary details, that it was successful; but no cigar. I tried playing around with the "visibility" property of the event (setting it to 'public'), but still no luck.

如果有人能帮我一把,我将不胜感激.

I'd really appreciate if someone can lend me a hand here.

[代码]

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"
}

推荐答案

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

It's creating the event in the calendar owned by the service account, if you want to manage it, you have to share the calendar with yourself (!):

$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);

参考:谁拥有服务帐户通过 Google Calendar API 创建的日历以及如何共享这些日历?

PS:有新版本的 PHP 客户端库.

PS: there's a new version of the PHP client library.

这篇关于Google Calendar API v3 - 不创建事件(服务器到服务器身份验证)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆