0

我已经激活了 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_id没有别的。oauth2_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://localhostoauth2_redirect_uri

4

1 回答 1

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 有点难找……查一下。例如,您可以在您的谷歌帐户主页/日历/齿轮符号/设置/单击日历名称/日历详细信息设置页面底部找到它。

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

于 2012-09-11T13:56:55.003 回答