Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

php - Google Calendar API v3 - Not Creating Event (Server-to-Server Authentication)

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.

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.

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

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.

[Code]

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

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

[Result]

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) "[email protected]"
  ["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"
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...