如何修复错误发送事件到谷歌日历使用api在php


How to fix error sending events to google calender using api in php

我试图从php使用api发送事件到谷歌日历。但这总是有一些错误。不知道下一步该做什么。下面是我的代码:

在/home/abcd/public_html/mouthworks/gplus-verifytoke -php-master/google-api-php-client/src/io/Google_REST.php:66 Stack trace: #0/home/abcd/public_html/mouthworks/gplus-verifytoke -php-master/google-api-php-client/src/io/Google_REST.php(36):Google_REST:: decodeHttpResponse(对象(Google_HttpRequest) # 1/home/abcd/public_html/mouthworks gplus-verifytoken-php-master/google-api-php-client/src/服务/Google_ServiceResource.php (186): Google_REST:执行(对象(Google_HttpRequest)) # 2/home/abcd/public_html/mouthworks/gplus-verifytoken-php-master/google-api-php-client/src/contrib/Google_CalendarService.php (494): Google_ServiceResource -> __call("插入",数组)# 3/home/abcd/public_html/mouthworks test.php (24):Google_EventsServiceResource ->插入("some_calendar@g…', Object(Google_Even)在/home/abcd/public_html/mouthworks/gplus-verifytoken-php-master/google-api-php-client/src/io/Google_REST.php第66行
        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;
        ?>

我在这里注意到的第一件事是你没有验证你的API调用,这就是为什么你得到错误(401)登录要求。必须首先对用户进行身份验证才能访问用户数据。请参考这里的文档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