2014-09-03 93 views
0

我無法連接能夠顯示事件,使用php客戶端庫插入事件。 這是我得到的錯誤。 我使用的客戶端庫我無法使用v3 php客戶端庫連接到谷歌日曆api

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/calendar/v3/calendars/primary?key=****************: (401) Login Required' in /var/www/html/google-api-php-client1/src/Google/Http/REST.php:79 
Stack trace:  
#0 /var/www/html/google-api-php-client1/src/Google/Http/REST.php(44): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request))  
#1 /var/www/html/google-api-php-client1/src/Google/Client.php(503): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))  
#2 /var/www/html/google-api-php-client1/src/Google/Service/Resource.php(195): Google_Client->execute(Object(Google_Http_Request))  
#3 /var/www/html/google-api-php-client1/src/Google/Service/Calendar.php(1269): Google_Service_Resource->call('get', Array, 'Google_Service_...')  
#4 /var/www/html/simple.php(27): Google_Service_Calendar_Calendars_Resource->get('primary') #5 {main} thrown in /var/www/html/google-api-php-client1/src/Google/Http/REST.php on line 79 

回答

0

的V3版本,因爲你得到401 login required錯誤,所以你需要在此處設置setAccessToken .visit Error with Google Calendar API - 401 Login required when adding a calendar event

可能是你需要使用原始身份驗證的訪問發出refreshToken()包含刷新令牌的代碼。 //例如

$google_client->refreshToken($token->refresh_token); 

,並使用'setDeveloperKey'.它works.you可以看到這一點:https://code.google.com/p/google-api-php-client/issues/detail?id=218

+0

能否請您分享給谷歌日曆與PHP客戶端庫 TIA這裏 – Naveen1425 2014-09-03 12:15:30

+0

訪問https://developers.google.com/google-apps/calendar/downloads – 2014-09-03 12:18:56

+0

那些工作不細的工作示例。 通過添加令牌代碼,它可以重定向我項目中的另一個測試頁面。 我如何將它重新導向到同一個sample.php頁面。 我注意到,在重定向之後,它在url中傳遞了一個「code」變量。 – Naveen1425 2014-09-03 12:25:52

0

終於解決了問題。 這是獲取日曆事件列表的最佳例子。

<?php 

// ... 
ini_set('display_errors', 1); 
error_reporting(E_ALL); 
set_include_path('google-api-php-client1/src'); 
require_once 'Google/Client.php'; 
require_once 'Google/Service/Calendar.php'; 
// 
$client = new Google_Client(); 
//print_r($client);exit; 
//$client->setUseObjects(true); 
$client->setApplicationName("your-app-name"); 
$client->setClientId("CLIENT id"); 
$client->setClientSecret('CLIENT SECRET'); 
$client->setDeveloperKey('DEVELOPER KEY'); 
$client->setRedirectUri('your url mostly localhost'); 
$client->setScopes(array(
    'https://www.googleapis.com/auth/calendar', 
    'https://www.googleapis.com/auth/calendar.readonly', 
)); 

if (isset($_GET['code'])) { 

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


if (isset($_SESSION['token'])) { 
    //echo 'innn';exit; 
    $client->setAccessToken($_SESSION['token']); 
    //echo 'innn';exit; 

} 
} 
if ($client->getAccessToken()) { 
    //echo 'innn';exit; 
    $service = new Google_Service_Calendar($client); 
     //echo '<pre>';  print_r($service);exit; 
    //$calendar = $service->calendars->get('primary'); 
    //echo $calendar->getSummary(); 

     $events = $service->events->listEvents('primary'); 

      while(true) { 
       foreach ($events->getItems() as $event) { 
        echo $event->getSummary(); 
          } 
        $pageToken = $events->getNextPageToken(); 
         if ($pageToken) { 
         $optParams = array('pageToken' => $pageToken); 
          $events = $service->events->listEvents('primary', $optParams); 
             } else { 
                break; 
               } 
         } 

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

} 

?> 
0

添加事件的腳本。 實際的問題是Google日曆版本3與其文檔不匹配。 所以我檢查Calender.php所有可用的類。

   $event = new Google_Service_Calendar_Event(); 
       $event->setSummary('Interview'); 
       $event->setLocation('Hell'); 
       $start = new Google_Service_Calendar_EventDateTime(); 
       $start->setDateTime('2014-09-04T10:00:00.000-07:00'); 
       $event->setStart($start); 
       $end = new Google_Service_Calendar_EventDateTime(); 
       $end->setDateTime('2014-09-04T11:00:00.000-07:00'); 
       $event->setEnd($end); 
       $attendee1 = new Google_Service_Calendar_EventAttendee(); 
       $attendee1->setEmail('[email protected]'); 
       // ... 
       $attendees = array($attendee1, 
            // ... 
           ); 
       $event->attendees = $attendees; 
       $createdEvent = $service->events->insert('primary', $event); 

       echo $createdEvent->getId();