谷歌日历访问从命令行


google calendar access from the command line

我已经激活了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

如何解决此问题?

哦,在google API控制台上,我有两个Redirect URIs:urn:ietf:wg:oauth:2.0:oobhttp://localhost的值,oauth2_redirect_uri应该使用哪一个?

我认为这可能是由几件事引起的。我的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有点找不到。。。查一下。例如,你可以在你的谷歌账户主页/日历/齿轮符号/设置/点击日历名称/日历详细信息设置页面底部找到它。

我的重定向url(不过是针对一个web应用程序)指向发出oauth请求的.php页面。如果经过身份验证,我会定义另一个重定向,这次是我自己的重定向。令人困惑是的,但它对我有效。