2

基本概要:我编写了一个客户端日历,允许人们安排约会并保留时间段(例如,预订记录在数据库中,第二个人不能选择相同的时间段)以便于联合和打印这些在提供商方面的约会,他们要求我将这些事件推送到一个谷歌日历。我已经为它创建了一个谷歌帐户和一个日历,然后创建了 API 密钥,可以在同一个谷歌用户下访问日历 API。所以我希望我的网站每次都使用这个用户的凭据来创建事件。看起来这将是一个“服务帐户”,但它似乎无权访问用户数据,甚至没有创建应用程序的用户。

关于如何解决这个问题的任何想法?如果看起来它应该非常简单,而且我不可能是第一个想要做这样的事情的人,但如果我能找到任何例子,我该死的。

这是代码片段

$event = new Google_Event();
$event->setSummary($title);
$event->setLocation($location);
$start = new Google_EventDateTime();
$start->setDateTime($date . 'T' . $startTime . ':00.000-06:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime($date . 'T' . $endTime . ':00.000-06:00');
$event->setEnd($end);
$attendee1 = new Google_EventAttendee();
$attendee1->setEmail($email);
$attendees = array($attendee1);
$event->attendees = $attendees;

$client = new Google_Client();
$service = new Google_CalendarService($client);

$createdEvent = $service->events->insert('my calendar ID', $event);

和错误

Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/projecthimcal@gmail.com/events?key=AIzaSyAfSCfLJCMSkGRmjZXRtChPPcMNmEuCZow: (401) Login Required' in /home/mydomain.com/wp-content/themes/mytheme/libs/gAPI/io/Google_REST.php:66
4

1 回答 1

1

也许为时已晚...但是您必须设置身份验证。

这是我用于我的代码,希望它可以帮助仍在寻找这个的人(注意我使用了api PHP客户端,类名可能与你的不同,但逻辑仍然相同):

    require_once 'Google/Client.php';
    require_once 'Google/Service/Calendar.php';
    session_start();

    $client = new Google_Client();
    $client->setApplicationName("Google Calendar PHP Starter Application");

    // 访问 https://code.google.com/apis/console?api=calendar 生成您的
    // 客户端 ID,客户端密码,并注册您的重定向 uri。
    $client->setClientId('');
    $client->setClientSecret('');
    $client->setRedirectUri('');
    $client->setDeveloperKey('');
    $client->setScopes(数组(
        'https://www.googleapis.com/auth/plus.me',
        'https://www.googleapis.com/auth/userinfo.email',
        'https://www.googleapis.com/auth/userinfo.profile',
        'https://www.googleapis.com/auth/calendar',
        'https://www.googleapis.com/auth/calendar.readonly'
    ));

    $cal = new Google_Service_Calendar($client);

    if (isset($_GET['logout'])) {
        未设置($_SESSION['token']);
    }

    if (isset($_GET['code'])) {
        $client->authenticate($_GET['code']);
        $_SESSION['token'] = $client->getAccessToken();
        header('位置:http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
    }

    if (isset($_SESSION['token'])) {
        $client->setAccessToken($_SESSION['token']);
    }

    if ($client->getAccessToken()) {            
        $event = new Google_Service_Calendar_Event();

        $event->setSummary($title);
        $event->setLocation($location);

        $start = new Google_Service_Calendar_EventDateTime();
        $start->setTimeZone('美国/蒙特利尔');
        $start->setDateTime($date . 'T' . $startTime . ':00.000-06:00');
        $event->setStart($start);

        $end = new Google_Service_Calendar_EventDateTime();
        $end->setTimeZone('美国/蒙特利尔');
        $end->setDateTime($date . 'T' . $endTime . ':00.000-06:00');
        $event->setEnd($end);


        $attendee1 = 新的 Google_Service_Calendar_EventAttendee();
        $attendee1->setEmail($email);
        $参加者=数组($参加者1);
        $事件->参加者= $参加者;

        $cal->events->insert($email, $event);

        $_SESSION['token'] = $client->getAccessToken();

    } 别的 {
        $authUrl = $client->createAuthUrl();
        print "<a class='login' href='$authUrl'>联系我!</a>";
    }

于 2014-05-21T17:37:31.677 回答