2013-04-17 64 views
0

即使點擊並連接並刷新瀏覽器,我也會每次獲得「連接我」鏈接。 此外,我得到這個錯誤,Google Calendar API PHP致命錯誤

正如在下面的答案建議,我已經做了修改,並在這裏添加它們。這導致了這個新的錯誤:

致命錯誤:未收到的異常'Google_ServiceException'帶消息'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}拋出/ 66#線上/######.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; 
} 
} 
+0

謝謝你的優秀代碼示例;你能編輯這個問題來提供更多的上下文嗎?這可能會幫助您獲得最佳答案。 – Dogoferis

+0

感謝您的提示....不知道我還需要補充什麼。它的Linux服務器......有幫助嗎? –

+0

你有沒有解決這個問題? – azz0r

回答

1

看來您使用的是$ service,您應該有$ cal。

所以$calendarList = $service->calendarList->listCalendarList();應該$calendarList = $cal->calendarList->listCalendarList();

$calendarList = $service->calendarList->listCalendarList($optParams);$calendarList = $cal->calendarList->listCalendarList($optParams);