为什么谷歌日历API v3 PHP在发布到日历时会抛出此错误,但在获取信息时却不会

时间:2012-04-02 17:39:55

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

我正在尝试使用Google Calendar API版本3(PHP)来管理日历。

轻松检索有关日历的信息,例如日历名称和日历列表,但我实际上“写入”不成功日历,例如添加活动或创建辅助日历。我总是在尝试中收到以下错误。

那里有人可以提供帮助吗?我在Google的文档中随处可见,但找不到任何答案。

错误(如果尝试发布,而不是检索信息):

致命错误:未捕获的异常'apiServiceException',并在/home/incs/google-api-php-client/src/io/apiREST.php中显示错误“错误调用POST https://www.googleapis.com/calendar/v3/calendars?key=eyfheufheuwehwi :( 400)无效值” :86堆栈跟踪:#0 /home/incs/google-api-php-client/src/io/apiREST.php(56):apiREST :: decodeHttpResponse(Object(apiHttpRequest))#1 / home / incs / google- api-php-client / src / service / apiServiceResource.php(187):apiREST :: execute(Object(apiServiceRequest))#2 /home/incs/google-api-php-client/src/contrib/apiCalendarService.php( 228):apiServiceResource-> __ call('insert',Array)#3 /home/public_html/gateway/google/index.php(130):CalendarsServiceResource-> insert(Object(Calendar))#4 {main}抛出第86行的/home/incs/google-api-php-client/src/io/apiREST.php

代码(顺便说一句,我的访问键在config.php中设置,因此注释了$ client行)

require("/home/incs/google-api-php-client/src/apiClient.php");
require("/home/incs/google-api-php-client/src/contrib/apiCalendarService.php");
session_start();

$client = new apiClient();
$client->setApplicationName("Google Calendar PHP Starter Application");

// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
// $client->setClientId('insert_your_oauth2_client_id');
// $client->setClientSecret('insert_your_oauth2_client_secret');
// $client->setRedirectUri('insert_your_oauth2_redirect_uri');
// $client->setDeveloperKey('insert_your_developer_key');
$service = new apiCalendarService($client);
if (isset($_GET['logout'])) 
{
  unset($_SESSION['token']);
}

if (isset($_GET['code'])) 
{
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}

if (isset($_SESSION['token'])) 
{
  $client->setAccessToken($_SESSION['token']);
}

if ($client->getAccessToken()) 
{

  $calendar = new Calendar();
  $calendar->setSummary('calendarSummary');
  $calendar->setTimeZone('American/Los_Angeles');

  $createdCalendar = $service->calendars->insert($calendar);


  $calList = $service->calendarList->listCalendarList();
  print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";


  $_SESSION['token'] = $client->getAccessToken();
} 
else 
{
  $authUrl = $client->createAuthUrl();
  print "<a class='login' href='$authUrl'>Connect Me!</a>";
}

1 个答案:

答案 0 :(得分:4)

就像现在一样,Google的代码示例中存在错误(我在这里获得了我粘贴的代码)。在该样本中,需要纠正以下内容才能使其起作用:

$calendar->setTimeZone('American/Los_Angeles'); 

需要更正

$calendar->setTimeZone('America/Los_Angeles');

然后不会抛出任何错误。