【问题标题】:Google Calendar API PHP fatal error谷歌日历 API PHP 致命错误
【发布时间】:2023-03-13 01:41:01
【问题描述】:

即使在单击并连接并刷新浏览器之后,我每次都会收到“连接我”链接。 另外,我收到此错误,

正如下面的回答所建议的,我已经做出了更改,并在此处添加了它们。这造成了这个新错误:

致命错误:####.org/google-api-php-client/src/io/Google_REST.php 中的未捕获异常“Google_ServiceException”和消息“调用 GET https://www.googleapis.com/calendar/v3/users/me/calendarList?key=AIzaSyDXm2epulkFrOgluO0dppEBknimmvpfKxY: (401) 需要登录” :66 堆栈跟踪:#0 ####.org/google-api-php-client/src/io/Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 ####。 org/google-api-php-client/src/service/Google_ServiceResource.php(177): Google_REST::execute(Object(Google_HttpRequest)) #2 #####/google-api-php-client/src/contrib /Google_CalendarService.php(154): Google_ServiceResource->__call('list', Array) #3 ####/avl/index2.php(44): Google_CalendarListServiceResource->listCalendarList() #4 {main} 抛出 /# #####.org/google-api-php-client/src/io/Google_REST.php 在第 66 行

<?php
require_once '../google-api-php-client/src/Google_Client.php';
require_once '../google-api-php-client/src/contrib/Google_CalendarService.php';
session_start();

$client = new Google_Client();
$client->setApplicationName("#####");

// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('327425587799.apps.googleusercontent.com');
$client->setClientSecret('*******');
$client->setRedirectUri('http://www.######.net/oauth2callback');
$client->setDeveloperKey('*******');
$client->setUseObjects(true); 

$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}

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

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

if ($client->getAccessToken()) {
$calList = $cal->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>";
}


$calendarList = $cal->calendarList->listCalendarList();
while(true){
foreach($calendarList->getItems()as $calendarListEntry){
  echo $calendarListEntry->getSummary();
}
$pageToken = $calendarList->getNextPageToken();
if($pageToken){
  $optParams = $cal->calendarList->listCalendarList($optParams);
}else{
  break;
}
}

【问题讨论】:

  • 感谢您提供出色的代码示例;您可以编辑问题以提供更多上下文吗?这可能会帮助您获得最佳答案。
  • 感谢您的提示....不知道我还需要添加什么。它是一个 Linux 服务器...这有帮助吗?
  • 你解决过这个问题吗?

标签: php google-calendar-api


【解决方案1】:

看来您在应该有 $cal 的地方使用 $service。

所以$calendarList = $service-&gt;calendarList-&gt;listCalendarList(); 应该是$calendarList = $cal-&gt;calendarList-&gt;listCalendarList();

$calendarList = $service-&gt;calendarList-&gt;listCalendarList($optParams); 应该是$calendarList = $cal-&gt;calendarList-&gt;listCalendarList($optParams);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 2019-09-08
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多