从命令行访问Google日历

时间:2012-09-09 08:27:17

标签: php oauth-2.0 google-calendar-api

我已激活Google API控制台https://code.google.com/apis/console,我已创建了一个项目,包括OAuth凭据。我选择了“桌面应用程序”设置,因为此应用程序将在浏览器外部运行,来自CLI (实际上仅在我的计算机上)。

我还按照此处所述下载了客户端库:https://code.google.com/p/google-api-php-client/

我已修改google-api-php-client/src/config.php修改了密钥application_nameoauth2_client_idoauth2_client_secretoauth2_redirect_urideveloper_key以及其他内容。

我也从services删除了所有其他服务。

现在,我有以下应用程序,从指南中的示例中汇总:

<?php
require_once 'google-api-php-client/src/apiClient.php';
require_once "google-api-php-client/src/contrib/apiCalendarService.php";

session_start();

$apiClient = new apiClient();
$apiClient->setUseObjects(true);
$service = new apiCalendarService($apiClient);


if (isset($_SESSION['oauth_access_token'])) {
  $apiClient->setAccessToken($_SESSION['oauth_access_token']);
} else {
  $token = $apiClient->authenticate();
  $_SESSION['oauth_access_token'] = $token;
}

$event = new Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new EventDateTime();
$start->setDateTime('2011-06-03T10:00:00.000-07:00');
$start->setTimeZone('America/Los_Angeles');
$event->setStart($start);
$end = new EventDateTime();
$end->setDateTime('2011-06-03T10:25:00.000-07:00');
$end->setTimeZone('America/Los_Angeles');
$event->setEnd($end);
$event->setRecurrence(array('RRULE:FREQ=WEEKLY;UNTIL=20110701T100000-07:00'));
$attendee1 = new EventAttendee();
$attendee1->setEmail('attendeeEmail');
// ...
$attendees = array($attendee1,
                   // ...
                   );
$event->attendees = $attendees;
$recurringEvent = $service->events->insert('primary', $event);

echo $recurringEvent->getId();

但是我收到以下错误:

Fatal error: Uncaught exception 'apiServiceException' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/primary/events?key=<the-key>: (401) Login Required' in /home/flav/projects/.../google-api-php-client/src/io/apiREST.php on line 86

apiServiceException: Error calling POST https://www.googleapis.com/calendar/v3/calendars/primary/events?key=<the-key>: (401) Login Required in /home/flav/projects/.../google-api-php-client/src/io/apiREST.php on line 86

Call Stack:
    0.0003     252600   1. {main}() /home/flav/projects/.../gcal-api.php:0
    0.0202    1769344   2. EventsServiceResource->insert() /home/flav/projects/.../gcal-api.php:38
    0.0202    1770288   3. apiServiceResource->__call() /home/flav/projects/.../google-api-php-client/src/contrib/apiCalendarService.php:493
    0.0206    1781864   4. apiREST::execute() /home/flav/projects/.../google-api-php-client/src/service/apiServiceResource.php:187
    0.2342    1794848   5. apiREST::decodeHttpResponse() /home/flav/projects/.../google-api-php-client/src/io/apiREST.php:56

如何解决这个问题?

哦,在谷歌API控制台上,我有Redirect URIs:urn:ietf:wg:oauth:2.0:oobhttp://localhost两个值,我应该将其用于oauth2_redirect_uri

1 个答案:

答案 0 :(得分:0)

我认为这可能是由几件事引起的。我的2美分:

您是否注册了几个日历?也许您想要插入事件的日历不是“主要”(=默认?)日历(或者您的Oauth信用数据是指不同的日历)。 如果是这样,请替换此行

$service->events->insert('primary', $event);

$calendar_id = "somelongstring@group.calendar.google.com"; //look this up from calendar /settings calendar details settings page at the bottom
$service->events->insert($calendar_id, $event);

日历ID有点难找...查找它。例如,您可以在谷歌帐户主页/日历/齿轮符号/设置/单击底部的日历名称/日历详细信息设置页面中找到它。

我的重定向网址(对于网络应用程序)指向发出oauth请求的.php页面。如果经过身份验证,我定义了另一个重定向,这次是我自己的。混乱?是的,但它对我有用。