2013-10-25 58 views
2

基本破壞:我已經編寫了一個客戶端日曆,允許人們安排約會和預約時間段(例如,預訂會記錄在數據庫和第二個人不能選擇相同的時間段),以方便在供應商方面聯合和打印這些約會,他們要求我將這些事件推送到單個Google日曆。我已經爲它創建了一個Google帳戶和一個日曆,然後創建了API密鑰,並在該Google用戶下訪問了Calendar API。所以我希望我的網站每次都使用此用戶的憑證來創建活動。看起來這將是一個「服務帳戶」,但似乎無法訪問用戶數據,甚至不是創建該應用程序的用戶。Google Calendar API v3 PHP - >嘗試將新事件創建爲單個日曆

如何退出這個功能的任何想法?如果它看起來應該是令人震驚的簡單,並且我不是第一個想要做這樣的事情的人,但是如果我能找到它的任何例子就該死。

下面的代碼

$event = new Google_Event(); 
$event->setSummary($title); 
$event->setLocation($location); 
$start = new Google_EventDateTime(); 
$start->setDateTime($date . 'T' . $startTime . ':00.000-06:00'); 
$event->setStart($start); 
$end = new Google_EventDateTime(); 
$end->setDateTime($date . 'T' . $endTime . ':00.000-06:00'); 
$event->setEnd($end); 
$attendee1 = new Google_EventAttendee(); 
$attendee1->setEmail($email); 
$attendees = array($attendee1); 
$event->attendees = $attendees; 

$client = new Google_Client(); 
$service = new Google_CalendarService($client); 

$createdEvent = $service->events->insert('my calendar ID', $event); 

和錯誤

Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/[email protected]/events?key=AIzaSyAfSCfLJCMSkGRmjZXRtChPPcMNmEuCZow: (401) Login Required' in /home/mydomain.com/wp-content/themes/mytheme/libs/gAPI/io/Google_REST.php:66 

回答

1

的片段也許是有點太晚了......但你必須建立一個認證。

下面是我用我的代碼,希望它能幫助人們仍在尋找這個(請注意,我用的api PHP client類名可能與你不同,但邏輯仍然是相同的):

 

    require_once 'Google/Client.php'; 
    require_once 'Google/Service/Calendar.php'; 
    session_start(); 

    $client = new Google_Client(); 
    $client->setApplicationName("Google Calendar PHP Starter Application"); 

    // Visit https://code.google.com/apis/console?api=calendar to generate your 
    // client id, client secret, and to register your redirect uri. 
    $client->setClientId(''); 
    $client->setClientSecret(''); 
    $client->setRedirectUri(''); 
    $client->setDeveloperKey(''); 
    $client->setScopes(array(
     'https://www.googleapis.com/auth/plus.me', 
     'https://www.googleapis.com/auth/userinfo.email', 
     'https://www.googleapis.com/auth/userinfo.profile', 
     'https://www.googleapis.com/auth/calendar', 
     'https://www.googleapis.com/auth/calendar.readonly' 
    )); 

    $cal = new Google_Service_Calendar($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()) {    
     $event = new Google_Service_Calendar_Event(); 

     $event->setSummary($title); 
     $event->setLocation($location); 

     $start = new Google_Service_Calendar_EventDateTime(); 
     $start->setTimeZone('America/Montreal'); 
     $start->setDateTime($date . 'T' . $startTime . ':00.000-06:00'); 
     $event->setStart($start); 

     $end = new Google_Service_Calendar_EventDateTime(); 
     $end->setTimeZone('America/Montreal'); 
     $end->setDateTime($date . 'T' . $endTime . ':00.000-06:00'); 
     $event->setEnd($end); 


     $attendee1 = new Google_Service_Calendar_EventAttendee(); 
     $attendee1->setEmail($email); 
     $attendees = array($attendee1); 
     $event->attendees = $attendees; 

     $cal->events->insert($email, $event); 

     $_SESSION['token'] = $client->getAccessToken(); 

    } else { 
     $authUrl = $client->createAuthUrl(); 
     print "<a class='login' href='$authUrl'>Connect me!</a>"; 
    } 

+1

謝謝Max。我使用REST API。我的最終代碼是[here](http://pastebin.com/rr4AMaHP) – scH

相關問題