drgmszo076956 2016-07-01 14:08
浏览 44

没有用户身份验证的Freebusy Google Calendar API PHP

I am creating a booking engine which uses Google calendar.

Needed:

  1. Get all events in time frame (day, week, month)
  2. Check availability for time frame, freebusy (30 min, hour)
  3. Post event if #2 was true

Currently I can get the events and check availability using the command line app I created following this tutorial by Google. But it still asks me to authenticate in browser, which is not what I want, as the calendar would always be the same and user won't have access to it.

My code so far:

<?php
require_once 'vendor/autoload.php';

define('APPLICATION_NAME', 'Google Calendar API PHP Quickstart');
define('CREDENTIALS_PATH', '~/.credentials/calendar-php-quickstart.json');
define('CLIENT_SECRET_PATH', '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_READONLY)
));

if (php_sapi_name() != 'cli') {
  throw new Exception('This application must be run on the command line.');
}

/**
 * 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 {
    // Request authorization from the user.
    $authUrl = $client->createAuthUrl();
    printf("Open the following link in your browser:
%s
", $authUrl);
    print 'Enter verification code: ';
    $authCode = trim(fgets(STDIN));

    // Exchange authorization code for an access token.
    $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
", $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);

// Print the next 10 events on the user's calendar.
$calendarId = 'pgsgh5dp4a5seur91fvn86bn3o@group.calendar.google.com';
$optParams = array(
  'maxResults' => 10,
  'orderBy' => 'startTime',
  'singleEvents' => TRUE,
  'timeMin' => '2005-08-15T15:52:01+0000',
);
$results = $service->events->listEvents($calendarId, $optParams);

if (count($results->getItems()) == 0) {
  print "No upcoming events found.
";
} else {
  print "Upcoming events:
";
  foreach ($results->getItems() as $event) {
    $start = $event->start->dateTime;
    if (empty($start)) {
      $start = $event->start->date;
    }
    printf("%s (%s)
", $event->getSummary(), $start);
  }
}
?>

Next steps:

  1. How to skip the client side authentication?
  2. How to turn everything into a web application instead of command line one?
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
    • ¥15 ARIMA模型时间序列预测用pathon解决
    • ¥15 孟德尔随机化怎样画共定位分析图
    • ¥18 模拟电路问题解答有偿速度
    • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
    • ¥15 误删注册表文件致win10无法开启
    • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
    • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
    • ¥50 html2canvas超出滚动条不显示
    • ¥15 java业务性能问题求解(sql,业务设计相关)