2014-09-03 96 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();