基本概述:我编写了一个客户端日历,允许人们安排约会并保留时间段(例如,预订记录在数据库中,第二个人不能选择相同的时间段)为了便于在提供者方进行联合和打印这些约会,他们要求我将这些事件推送到单个谷歌日历。我已经为它创建了一个谷歌帐户和日历,然后在同一个谷歌用户下创建了可以访问Calendar 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
答案 0 :(得分:1)
也许有点太晚了......但你必须设置一个身份验证。
这是我用于我的代码,希望它可以帮助人们仍在寻找这个(注意我使用api PHP client类名可能与你的不同但逻辑仍然相同):
require_once 'Google/Client.php'; require_once 'Google/Service/Calendar.php'; session_start(); $client = new Google_Client(); $client->setApplicationName("Google Calendar PHP Starter Application"); // Visit https://code.google.com/apis/console?api=calendar to generate your // client id, client secret, and to register your redirect uri. $client->setClientId(''); $client->setClientSecret(''); $client->setRedirectUri(''); $client->setDeveloperKey(''); $client->setScopes(array( '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'])) { unset($_SESSION['token']); } if (isset($_GET['code'])) { $client->authenticate($_GET['code']); $_SESSION['token'] = $client->getAccessToken(); header('Location: 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('America/Montreal'); $start->setDateTime($date . 'T' . $startTime . ':00.000-06:00'); $event->setStart($start); $end = new Google_Service_Calendar_EventDateTime(); $end->setTimeZone('America/Montreal'); $end->setDateTime($date . 'T' . $endTime . ':00.000-06:00'); $event->setEnd($end); $attendee1 = new Google_Service_Calendar_EventAttendee(); $attendee1->setEmail($email); $attendees = array($attendee1); $event->attendees = $attendees; $cal->events->insert($email, $event); $_SESSION['token'] = $client->getAccessToken(); } else { $authUrl = $client->createAuthUrl(); print "<a class='login' href='$authUrl'>Connect me!</a>"; }