2016-05-25 99 views
2

我已經創建了一個PHP頁面fullcalendar jquery插件,它允許網站用戶預訂時間。在後臺我將事件存儲到我的數據庫中,然後使用Google日曆API爲我的Google日曆創建相同的事件。從我的網站添加事件到我的谷歌日曆

但我的實現是要求用戶登錄到他們的谷歌帳戶,並在用戶的日曆上創建事件。我怎樣才能實現在我的(網站所有者)谷歌日曆「無縫」創建用戶事件?

我的代碼如下:

<?php 
session_start(); 

require_once __DIR__ . '/vendor/autoload.php'; 

define('APPLICATION_NAME', 'Google Calendar'); 
define('CREDENTIALS_PATH', '~/.credentials/calendar-php-quickstart.json'); 
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json'); 
// If modifying these scopes, delete your previously saved credentials 
// at ~/.credentials/calendar-php-quickstart.json 
define('SCOPES', implode(' ', array(
    Google_Service_Calendar::CALENDAR) 
)); 


/** 
* Returns an authorized API client. 
* @return Google_Client the authorized client object 
*/ 
function getClient() { 
    $client = new Google_Client(); 
    $client->setApplicationName(APPLICATION_NAME); 
    $client->setScopes(SCOPES); 
    $client->setAuthConfigFile(CLIENT_SECRET_PATH); 
    $client->setAccessType('offline'); 
    // Load previously authorized credentials from a file. 
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH); 

/*if (file_exists($credentialsPath)) { 
    $accessToken = file_get_contents($credentialsPath); 
} else {*/ 

    if(!$_REQUEST['code']) { 
     $authUrl = $client->createAuthUrl(); 
     //printf("Open the following link in your browser:\n%s\n", $authUrl); 
     header("Location:".$authUrl); 
    } 
    $authCode = $_REQUEST['code']; 
    $accessToken = $client->authenticate($authCode); 

    // Store the credentials to disk. 
    /*if(!file_exists(dirname($credentialsPath))) { 
     mkdir(dirname($credentialsPath), 0700, true); 
    } 
    file_put_contents($credentialsPath, $accessToken);*/ 
    //printf("Credentials saved to %s\n", $credentialsPath); 
//} 
$client->setAccessToken($accessToken); 

// Refresh the token if it's expired. 
if ($client->isAccessTokenExpired()) { 
$client->refreshToken($client->getRefreshToken()); 
file_put_contents($credentialsPath, $client->getAccessToken()); 
} 
return $client; 
} 
/** 
* Expands the home directory alias '~' to the full path. 
* @param string $path the path to expand. 
* @return string the expanded path. 
*/ 
function expandHomeDirectory($path) { 
    $homeDirectory = getenv('HOME'); 
    if (empty($homeDirectory)) { 
    $homeDirectory = getenv("HOMEDRIVE") . getenv("HOMEPATH"); 
    } 
    return str_replace('~', realpath($homeDirectory), $path); 
} 

// Get the API client and construct the service object. 
$client = getClient(); 
$service = new Google_Service_Calendar($client); 


$event = new Google_Service_Calendar_Event(array(
    'summary' => $_SESSION['cal-data']['fname']." ".$_SESSION['cal-data']['lname'], 
    'location' => $_SESSION['cal-data']['address'], 
    'description' => "Contact: ".$_SESSION['cal-data']['phoneno']." for ".$_SESSION['cal-data']['title'], 
    'start' => array(
    'dateTime' => $_SESSION['cal-data']['starttm'], 
    //'timeZone' => 'America/Los_Angeles', 
), 
    'end' => array(
    'dateTime' => $_SESSION['cal-data']['endtm'], 
    //'timeZone' => 'America/Los_Angeles', 
), 
    /*'recurrence' => array(
    'RRULE:FREQ=DAILY;COUNT=2' 
),*/ 
    'attendees' => array(
    array('email' => $_SESSION['cal-data']['email']) 
), 
    'reminders' => array(
    'useDefault' => FALSE, 
    'overrides' => array(
     array('method' => 'email', 'minutes' => 24 * 60), 
     array('method' => 'popup', 'minutes' => 10), 
    ), 
), 
)); 

$calendarId = '[email protected]'; 
$event = $service->events->insert($calendarId, $event); 
//printf('Event created: %s\n', $event->htmlLink); 
unset($_SESSION['cal-data']); 

header("Location: next-page.php"); 
+0

應該有一個公開的設置,不是嗎?我從來沒有與這些工作過,但假設會有。 –

+0

是的,您可以公開您的日曆並且我的日曆已經公開。 – Paks

回答

0

有存儲在共享/公共日曆憑證的方式,使用日曆ID爲「正常」的API調用。

POST https://www.googleapis.com/calendar/v3/calendars/YOUR_SHARED_CALENDAR_ID/events

,但你要分享的人或與公衆的日曆。 https://support.google.com/calendar/answer/37082?hl=en

,否則你不能

+0

我已將我的日曆設爲公開。 – Paks

+0

你能幫我在我的代碼中,我應該在哪裏按照你的建議進行api調用? – Paks

0

您可以使用服務帳戶作爲代理用戶訪問您的日曆(不提示用戶登錄自己的帳戶)。基於Inserting Google Calendar Entries with Service Account,您可以將事件添加到共享日曆。

下面是步驟:

  1. Create a Service Account
  2. 你需要創建一個公鑰/私鑰對。

在創建服務帳戶窗口,爲服務帳戶的名稱,並選擇提供新的私鑰。如果您要將Google Apps域範圍授權授予服務帳戶,請選擇啓用Google Apps域範圍的授權。

注:Enable the API和共享日曆,從服務帳戶憑據您的電子郵件地址。不要忘記啓用「更改事件」。