简体   繁体   English

Google Calendar API PHP致命错误

[英]Google Calendar API PHP fatal error

I get the "Connect Me" link every time, even after clicking and connecting, and refreshing browser. 我每次都获得“ Connect Me”链接,即使单击并连接并刷新浏览器也是如此。 Also, I get this error, 另外,我得到这个错误,

As suggested in answer below, I have made the changes, and added them here. 如下面的答案所示,我已经进行了更改,并将其添加到此处。 this has created this new error: 这造成了这个新的错误:

Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling GET https://www.googleapis.com/calendar/v3/users/me/calendarList?key=AIzaSyDXm2epulkFrOgluO0dppEBknimmvpfKxY : (401) Login Required' in ####.org/google-api-php-client/src/io/Google_REST.php:66 Stack trace: #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} thrown in /######.org/google-api-php-client/src/io/Google_REST.php on line 66 致命错误:未捕获的异常“ Google_ServiceException”,并在####中显示消息“调用GET https://www.googleapis.com/calendar/v3/users/me/calendarList?key=AIzaSyDXm2epulkFrOgluO0dppEBknimmvpfKxY:(401 )需要登录”时出错。 org / google-api-php-client / src / io / Google_REST.php: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中

<?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;
}
}

It appears you are using $service where you should have $cal. 看来您使用的是$ service,应该有$ cal。

so $calendarList = $service->calendarList->listCalendarList(); 所以$calendarList = $service->calendarList->listCalendarList(); should be $calendarList = $cal->calendarList->listCalendarList(); 应该是$calendarList = $cal->calendarList->listCalendarList();

and $calendarList = $service->calendarList->listCalendarList($optParams); $calendarList = $service->calendarList->listCalendarList($optParams); should be $calendarList = $cal->calendarList->listCalendarList($optParams); 应该是$calendarList = $cal->calendarList->listCalendarList($optParams);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM