在php中使用api将事件发送到谷歌日历时出错

时间:2016-11-11 05:37:47

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

我正在尝试使用来自php的API将事件发送到谷歌日历。但总有一些错误。无法理解下一步该做什么。 这是我的代码:
         

            require_once './gplus-verifytoken-php-master/
            google-api-php-client/src/Google_Client.php';
            require_once '
            ./gplus-verifytoken-php-master/
            google-api-php- client/src/contrib/Google_CalendarService.php';

            session_start();

            ob_start();
            $client = new Google_Client();
            $client->setApplicationName('demo');
            $client->
            setClientId('client id');
            $client->setClientSecret('secret');
            $client->setRedirectUri('http://someurl.com');
            $client->
            setDeveloperKey('dev key');
            $cal = new Google_CalendarService($client);

            $event = new Google_Event();
            $event->setSummary('Pi Day');
            $event->setLocation('Math Classroom');
            $start = new Google_EventDateTime();
            $start->setDateTime('2016-11-14T10:00:00.000-05:00');
            $event->setStart($start);
            $end = new Google_EventDateTime();
            $end->setDateTime('2016-11-14T10:25:00.000-05:00');
            $event->setEnd($end);

            // error is on this next line
           $createdEvent = 
           $cal->events->insert('some_calendar@gmail.com',$event);

            echo $createdEvent->id;

            ?>

请帮忙。谢谢。

2 个答案:

答案 0 :(得分:0)

我在这里注意的第一件事是你没有验证你的API调用,这就是你可能收到错误的原因。您必须首先验证用户是否可以访问用户数据。请参阅此处的文档https://developers.google.com/api-client-library/php/auth/web-app。用户成功通过身份验证后,即可进行API调用。我注意到的另一件事是你将电子邮件地址放在日历ID上。如果要访问当前登录用户的主日历,请使用“primary”关键字。您的代码应如下所示:

session_start();

$client = new Google_Client();
$client->setAuthConfig("client_secrets.json");
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/event.php');
$client->addScope("https://www.googleapis.com/auth/calendar");

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {

    $client->setAccessToken($_SESSION['access_token']);

    $cal = new Google_Service_Calendar($client);

    $event = new Google_Service_Calendar_Event(array(
        'summary' => 'Pi Day',
        'location' => 'Math Classroom',
        'description' => 'Pi History in detail',
        'start' => array(
            'dateTime' => '2016-11-14T10:00:00-05:00'   
        ),
        'end' => array(
            'dateTime' => '2016-11-14T10:25:00-05:00'
        ),  
        'reminders' => array(
        'useDefault' => FALSE,
        'overrides' => array(
            array('method' => 'email', 'minutes' => 24 * 60),
            array('method' => 'popup', 'minutes' => 10),
        ),
      ),
    ));

    $calendarId = 'primary';
    $event = $cal->events->insert($calendarId, $event);
    printf('Event created: %s\n', $event->htmlLink);

} else {

    if (!isset($_GET['code'])) {    

          $auth_url = $client->createAuthUrl();
          header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));

    } else {  

      $client->authenticate($_GET['code']);
      $_SESSION['access_token'] = $client->getAccessToken();

      $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/event.php';
      header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));

    }

}

我希望您觉得此信息有用。我还建议您阅读https://developers.google.com/google-apps/calendar/v3/reference/events/insert

中的参考文档

答案 1 :(得分:-1)

请参阅以下链接,并且必须阅读每个功能说明及其用法:

https://developers.google.com/google-apps/calendar/create-events

下面是google-api-link的示例代码

// Refer to the PHP quickstart on how to setup the environment:
// https://developers.google.com/google-apps/calendar/quickstart/php
// Change the scope to Google_Service_Calendar::CALENDAR and delete any stored
// credentials.

$event = new Google_Service_Calendar_Event(array(
  'summary' => 'Google I/O 2015',
  'location' => '800 Howard St., San Francisco, CA 94103',
  'description' => 'A chance to hear more about Google\'s developer products.',
  'start' => array(
    'dateTime' => '2015-05-28T09:00:00-07:00',
    'timeZone' => 'America/Los_Angeles',
  ),
  'end' => array(
    'dateTime' => '2015-05-28T17:00:00-07:00',
    'timeZone' => 'America/Los_Angeles',
  ),
  'recurrence' => array(
    'RRULE:FREQ=DAILY;COUNT=2'
  ),
  'attendees' => array(
    array('email' => 'lpage@example.com'),
    array('email' => 'sbrin@example.com'),
  ),
  'reminders' => array(
    'useDefault' => FALSE,
    'overrides' => array(
      array('method' => 'email', 'minutes' => 24 * 60),
      array('method' => 'popup', 'minutes' => 10),
    ),
  ),
));

$calendarId = 'primary';
$event = $service->events->insert($calendarId, $event);
printf('Event created: %s\n', $event->htmlLink);