从我的网站向我的Google日历添加活动

时间:2016-05-25 19:07:43

标签: php fullcalendar google-calendar-api

我创建了一个带有fullcalendar jquery插件的php页面,允许网站用户预订时间。在后台我将事件存储到我的数据库中,然后使用Google日历API为我的Google日历创建相同的事件。

但我的实施是要求用户登录他们的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 = 'xxxxxxxxxxxxxxxxxxxxx@group.calendar.google.com';
$event = $service->events->insert($calendarId, $event);
//printf('Event created: %s\n', $event->htmlLink);
unset($_SESSION['cal-data']);

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

2 个答案:

答案 0 :(得分:0)

有一种方法可以在共享/公共日历中存储凭据,使用日历ID,例如&#34; normal&#34; API调用。

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

但是,您必须与这些人或公众共享日历。 https://support.google.com/calendar/answer/37082?hl=en

否则你不能

答案 1 :(得分:0)

您可以使用服务帐户作为访问日历的代理用户(不再提示用户登录其帐户)。根据{{​​3}},您可以将事件添加到共享日历。

以下是步骤:

  1. Inserting Google Calendar Entries with Service Account
  2. 您需要创建公钥/私钥对。
  3.   

    在“创建服务帐户”窗口中,键入服务帐户的名称,然后选择“提供新的私钥”。如果您要向服务帐户授予Google Apps域范围的权限,请同时选择启用Google Apps域范围的委派。

    注意: Create a Service Account并通过服务帐户凭据将日历分享到您的电子邮件地址。并且不要忘记启用&#34;对事件进行更改&#34;。