2012-09-09 123 views
0

我已經激活了谷歌API控制檯https://code.google.com/apis/console,並且我創建了一個項目,包括OAuth憑證。我選擇了「桌面應用程序」設置,因爲此應用程序將在CLI(實際上僅在我的計算機上)的瀏覽器之外運行。從命令行訪問谷歌日曆

我也下載了這裏描述的客戶端庫:https://code.google.com/p/google-api-php-client/

我已經編輯google-api-php-client/src/config.php編輯鍵application_nameoauth2_client_idoauth2_client_secretoauth2_redirect_urideveloper_key,沒有別的。

我也刪除了services的所有其他服務。

現在,我有以下的應用,從樣品放在一起指南:

<?php 
require_once 'google-api-php-client/src/apiClient.php'; 
require_once "google-api-php-client/src/contrib/apiCalendarService.php"; 

session_start(); 

$apiClient = new apiClient(); 
$apiClient->setUseObjects(true); 
$service = new apiCalendarService($apiClient); 


if (isset($_SESSION['oauth_access_token'])) { 
    $apiClient->setAccessToken($_SESSION['oauth_access_token']); 
} else { 
    $token = $apiClient->authenticate(); 
    $_SESSION['oauth_access_token'] = $token; 
} 

$event = new Event(); 
$event->setSummary('Appointment'); 
$event->setLocation('Somewhere'); 
$start = new EventDateTime(); 
$start->setDateTime('2011-06-03T10:00:00.000-07:00'); 
$start->setTimeZone('America/Los_Angeles'); 
$event->setStart($start); 
$end = new EventDateTime(); 
$end->setDateTime('2011-06-03T10:25:00.000-07:00'); 
$end->setTimeZone('America/Los_Angeles'); 
$event->setEnd($end); 
$event->setRecurrence(array('RRULE:FREQ=WEEKLY;UNTIL=20110701T100000-07:00')); 
$attendee1 = new EventAttendee(); 
$attendee1->setEmail('attendeeEmail'); 
// ... 
$attendees = array($attendee1, 
        // ... 
        ); 
$event->attendees = $attendees; 
$recurringEvent = $service->events->insert('primary', $event); 

echo $recurringEvent->getId(); 

不過,我發現了以下錯誤:

Fatal error: Uncaught exception 'apiServiceException' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/primary/events?key=<the-key>: (401) Login Required' in /home/flav/projects/.../google-api-php-client/src/io/apiREST.php on line 86 

apiServiceException: Error calling POST https://www.googleapis.com/calendar/v3/calendars/primary/events?key=<the-key>: (401) Login Required in /home/flav/projects/.../google-api-php-client/src/io/apiREST.php on line 86 

Call Stack: 
    0.0003  252600 1. {main}() /home/flav/projects/.../gcal-api.php:0 
    0.0202 1769344 2. EventsServiceResource->insert() /home/flav/projects/.../gcal-api.php:38 
    0.0202 1770288 3. apiServiceResource->__call() /home/flav/projects/.../google-api-php-client/src/contrib/apiCalendarService.php:493 
    0.0206 1781864 4. apiREST::execute() /home/flav/projects/.../google-api-php-client/src/service/apiServiceResource.php:187 
    0.2342 1794848 5. apiREST::decodeHttpResponse() /home/flav/projects/.../google-api-php-client/src/io/apiREST.php:56 

如何解決這一問題?

哦,和谷歌API控制檯上,我有Redirect URIs:urn:ietf:wg:oauth:2.0:oobhttp://localhost兩個值,哪一個我應該使用oauth2_redirect_uri

回答

0

我認爲這可能是由幾件事引起的。我的2美分:

你有幾個日曆註冊?也許你想插入事件的日曆不是「主」(=默認?)日曆(或者你的Oauth信用數據指的是不同的日曆)。 如果是這樣,替換此行

$service->events->insert('primary', $event); 

$calendar_id = "[email protected]"; //look this up from calendar /settings calendar details settings page at the bottom 
$service->events->insert($calendar_id, $event); 

日曆ID是有點難找......看看它。例如,您可以在Google帳戶主頁/日曆/齒輪符號/設置/點擊日曆名稱/日曆詳細信息設置頁面的底部找到它。

我的重定向url(對於web應用程序,雖然)指向發出oauth請求的.php頁面。如果通過身份驗證,我定義了另一個重定向,這次是我自己的。混亂?是的,但它適用於我。