繁体   English   中英

无法使用google-api-php-client在特定日历中插入事件

[英]Impossible to insert event in specific calendar using google-api-php-client

我尝试在特定日历中使用google-api-php-client插入事件。 如果我设置为“主要”,而不是“日历ID”,则该日历正在运行,但是在主日历中看不到该事件。

我试图指定一个日历ID,例如co-xxxxx.ch_lhr1ojekr990m475kv6nilueqs@group.calendar.google.com,我遇到以下错误:

致命错误:未捕获的异常“ Google_Service_Exception”,消息为“调用POST https://www.googleapis.com/calendar/v3/calendars/xxxxx8egm4tng6rlj8usmavdu8%40google.com/events时出错:(404)找不到” client / client1 / web347 / web / google-api-php / src / Google / Http / REST.php:111堆栈跟踪:#0 / var / www / clients / client1 / web347 / web / google-api-php / src /Google/Http/REST.php(63):Google_Http_REST ::

这是我的代码。 身份验证确实有效

    $cal_id = 'primary';  // IT WORKS, BUT I DON'T SEE THE EVENT IN MAIN CALENDAR
    $cal_id = 'xxxxx38egm4tng6rlj8usmavdu8@google.com'; // IT CAUSES THE ERROR

    //Set the Event data
    $event = new Google_Service_Calendar_Event();
    $event->setSummary($title);
    $event->setDescription($desc);

    $ev_date = '2015-01-05T08:00:00+01:00';

    $start = new Google_Service_Calendar_EventDateTime();
    $start->setTimeZone('Europe/London');
    $start->setDateTime('2015-01-01T19:15:00');
    $event->setStart($start);

    $end = new Google_Service_Calendar_EventDateTime();
    $end->setTimeZone('Europe/London');
    $end->setDateTime('2015-01-05T20:15:00');
    $event->setEnd($end);

    $createdEvent = $calendarService->events->insert( $cal_id, $event);
    $event = $calendarService->events->get($cal_id, $createdEvent->getId());
    //
    if ($event != null) {
        echo "Inserted:";
        echo "EventID=".$event->getId();
        echo "Summary=".$event->getSummary();
        echo "Status=".$event->getStatus();
     }

$ createdEvent使用“主”日历返回了此代码

Google_Service_Calendar_Event Object
(
[collection_key:protected] => recurrence
[internal_gapi_mappings:protected] => Array
    (
    )

[anyoneCanAddSelf] => 
[attendeesType:protected] => Google_Service_Calendar_EventAttendee
[attendeesDataType:protected] => array
[attendeesOmitted] => 
[colorId] => 
[created] => 2015-01-05T11:21:03.000Z
[creatorType:protected] => Google_Service_Calendar_EventCreator
[creatorDataType:protected] => 
[description] => test
[endType:protected] => Google_Service_Calendar_EventDateTime
[endDataType:protected] => 
[endTimeUnspecified] => 
[etag] => "2840913726324000"
[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=M2p2ZXY1cmZpY2Y3dTk5MjQ0ZmNsYW9xNTggMTAwNjUzNTQ3ODk1My1laTI5a2M4cGQxZHE3cDUycGhtdmRlMWs3dXZkZjIzb0BkZXZlbG9wZXIuZ3NlcnZpY2VhY2NvdW50LmNvbQ
[iCalUID] => 3jvev5rficf7u99244fclaoq58@google.com
[id] => 3jvev5rficf7u99244fclaoq58
[kind] => calendar#event
[location] => 
[locked] => 
[organizerType:protected] => Google_Service_Calendar_EventOrganizer
[organizerDataType:protected] => 
[originalStartTimeType:protected] => Google_Service_Calendar_EventDateTime
[originalStartTimeDataType:protected] => 
[privateCopy] => 
[recurrence] => 
[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] => test
[transparency] => 
[updated] => 2015-01-05T11:21:03.162Z
[visibility] => 
[modelData:protected] => Array
    (
        [creator] => Array
            (
                [email] => 1006535478953-ei29kc8pd1dq7p52phmvde1k7uvdf23o@developer.gserviceaccount.com
                [self] => 1
            )

        [organizer] => Array
            (
                [email] => 1006535478953-ei29kc8pd1dq7p52phmvde1k7uvdf23o@developer.gserviceaccount.com
                [self] => 1
            )

        [start] => Array
            (
                [dateTime] => 2015-01-01T19:15:00Z
                [timeZone] => Europe/London
            )

        [end] => Array
            (
                [dateTime] => 2015-01-05T20:15:00Z
                [timeZone] => Europe/London
            )

        [reminders] => Array
            (
                [useDefault] => 1
            )

    )

[processed:protected] => Array
    (
    )

)

首先您使用以下方法获取日历列表

print_r($calendarList = $calendarService->calendarList->listCalendarList()); 

然后获取您特定日历的ID,您可以通过

$createdEvent = $calendarService->events->insert('NEW_ID', $event);
    <?php
    error_reporting(1);
    require_once 'src/Google_Client.php';
    require_once 'src/contrib/Google_CalendarService.php';

    $client = new Google_Client();
    // Get your credentials from the console
    $client->setClientId('XXXXXXXXXX');
    $client->setClientSecret('XXXXXXXXX');
    $client->setRedirectUri('XXXXXXXXXX');

    $client->setScopes(array('https://www.googleapis.com/auth/calendar')); //Calender Scope 

   $cal=new Google_CalendarService($client);

   $authUrl = $client->createAuthUrl();

   $authCode = trim(fgets(STDIN));

  if(!file_exists("token.json") || filesize("token.json")==0) 
  {
        // Save token for future use

        $accessToken = $client->authenticate($authCode);      
        $client->setAccessToken($accessToken);
        if ($client->getAccessToken()) {
            $token = $client->getAccessToken();
            $authObj = json_decode($token);
            $refreshToken = $authObj->refresh_token;
        }
        file_put_contents("token.json",$refreshToken);  
    }
    else 
    {
        $accessToken = file_get_contents("token.json");
        //echo "hi:";
        $client->refreshToken($accessToken);
        $client->getAccessToken();
    }
    //Create Calender Event
        $event = new Google_Event();
        $event->setSummary('Appointment');
        $event->setLocation('Rajkot');
        //$event->sendNotifications('TRUE');
        $start = new Google_EventDateTime();
        $start->setDateTime('2014-12-24T11:20:00.000+05:30');
        $event->setStart($start);
        $end = new Google_EventDateTime();
        $end->setDateTime('2014-12-24T11:30:00.000+05:30');
        $event->setEnd($end);
        $event->setHangoutLink(null);

            $reminderI = new Google_EventReminder();
            $reminderI->setMethod('popup');
            $reminderI->setMinutes('5');

            $reminderII = new Google_EventReminder();
            $reminderII->setMethod('email');
            $reminderII->setMinutes('10');

        $reminder = new Google_EventReminders();
        $reminder->setUseDefault('false');
        $reminder->setOverrides(array($reminderI,$reminderII));
        $event->setReminders($reminder);

        $createdEvent = $cal->events->insert('primary', $event);

        print_r($createdEvent);
    ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM