2015-07-12 80 views
1

我需要授權我網站上的用戶在我們的Google日曆中插入活動。這是一個預訂系統,用戶選擇一項服務,確認並在我們的日曆中自動插入新事件。 問題是,我有我不需要的客戶端頁面上的授權按鈕。Google Calendar API未經授權插入活動

<button id="authorize-button" onclick="handleAuthClick(event)">Authorize</button> 

所以我想我需要一個服務帳戶授權。但我不知道如何在JavaScript中使用生成的json和p12鍵。 我需要一個樣品或至少明白我必須做的..

JS代碼:

​​3210

附: - 網站是在PHP。

回答

0

是的,我需要使用服務帳戶與PHP SDK。你下載P12鍵,上載服務器,然後:

require_once ('../src/Google/autoload.php'); 

$client_id = '{your_code}.apps.googleusercontent.com'; //Client ID 
$service_account_name = '{your_email}@developer.gserviceaccount.com'; //Email Address 
$key_file_location = $_SERVER['DOCUMENT_ROOT'] .'{file}.p12'; //key.p12 
$client = new Google_Client(); //AUTHORIZE OBJECTS 
$client->setApplicationName("App Name"); 

//INSTATIATE NEEDED OBJECTS (In this case, for freeBusy query, and Create New Event) 
$service = new Google_Service_Calendar($client); 
$id = new Google_Service_Calendar_FreeBusyRequestItem($client); 
$item = new Google_Service_Calendar_FreeBusyRequest($client); 
$event = new Google_Service_Calendar_Event($client); 
$startT = new Google_Service_Calendar_EventDateTime($client); 
$endT = new Google_Service_Calendar_EventDateTime($client); 


if (isset($_SESSION['service_token'])) { 
    $client->setAccessToken($_SESSION['service_token']); 
} 
$key = file_get_contents($key_file_location); 
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name, 
    array('https://www.googleapis.com/auth/calendar'), 
    $key 
); 
$client->setAssertionCredentials($cred); 
if ($client->getAuth()->isAccessTokenExpired()) { 
    $client->getAuth()->refreshTokenWithAssertion($cred); 
} 
$_SESSION['service_token'] = $client->getAccessToken(); 

/************************************************ 
    MAIN FUNCTIONS 
************************************************/ 

function GetFreeBusy($calendar_id, $calendar_date) { 
    global $id; //GET OBJECTS FROM OUTSIDE 
    global $item; 
    global $service; 
    $arrayTime = array(); 
    $id->setId($calendar_id); 
    $item->setItems(array($id)); 
    $item->setTimeZone('Europe/Rome'); 
    $item->setTimeMax("{$calendar_date}T18:00:00+02:00"); 
    $item->setTimeMin("{$calendar_date}T08:00:00+02:00"); 
    $query = $service->freebusy->query($item); 

    $start = $query["calendars"][$calendar_id]["busy"]; 
    $end = $query["calendars"][$calendar_id]["busy"]; 

    $length = count($start); 
    for ($i = 0; $i < $length; $i++) { 
    $startTime = $start[$i]["start"]; 
    $endTime = $start[$i]["end"]; 

    list($a, $b) = explode('T', $startTime); 
    list($startHour, $d) = explode(':00+', $b); 
    list($e, $f) = explode('T', $endTime); 
    list($endHour, $g) = explode(':00+', $f); 
    array_push($arrayTime, array($startHour, $endHour)); 
    // I CREATED AN ARRAY FOR MY NEEDS ex. [ ["8:00", "10:00"], ["14:00", "14:30"] ] 
    } 
    return $arrayTime; 
} 

function CreateEvent($calendarId, $summary, $location, $description, $date, $start, $end) { 

    global $service; 
    global $event; 
    global $startT; 
    global $endT; 

    $startT->setTimeZone("Europe/Rome"); 
    $startT->setDateTime($date."T".$start.":00"); 
    $endT->setTimeZone("Europe/Rome"); 
    $endT->setDateTime($date."T".$end.":00"); 

    $event->setSummary($summary); 
    $event->setLocation($location); 
    $event->setDescription($description); 
    $event->setStart($startT); 
    $event->setEnd($endT); 

if($insert = $service->events->insert($calendarId, $event)) { 
    return true; 
} 

} 

你打開你的日曆,並添加共享給{your_email}@developer.gserviceaccount.com。現在,任何用戶都可以爲我的cal創建事件。