【问题标题】:Google Calendar API v3 PHP -> Trying to create new events into a single calendarGoogle Calendar API v3 PHP -> 尝试在单个日历中创建新事件
【发布时间】:2013-10-25 12:56:26
【问题描述】:

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

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

这是代码的 sn-p

$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

【问题讨论】:

    标签: php google-calendar-api service-accounts


    【解决方案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"); // 访问 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 ""; }

    【讨论】:

    • 感谢马克斯。我选择了 REST API。我的最终代码是here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    相关资源
    最近更新 更多