【问题标题】:google calendar access from the command line从命令行访问谷歌日历
【发布时间】:2012-09-11 22:24:42
【问题描述】:

我已经激活了 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 使用哪一个?

【问题讨论】:

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


    【解决方案1】:

    我认为这可能是由几件事造成的。我的 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(不过对于网络应用程序)指向发出 oauth 请求的 .php 页面。如果经过身份验证,我会定义另一个重定向,这次是我自己的。令人困惑?是的,但它对我有用。

    【讨论】:

      猜你喜欢
      • 2017-01-21
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-01
      • 2013-01-01
      • 1970-01-01
      相关资源
      最近更新 更多