如何创建谷歌日历条目w/o用户


How to create Google calendar entry w/o user?

最近我决定在我的网站上为用户提供以下功能:在我的员工日历中在线预订时段。所有员工都有在谷歌应用程序(免费版)中链接到我们域的谷歌帐户。用户在一些前端进行预订(例如,不直接在员工谷歌日历中),然后在我们的PHP服务器上处理请求,如果请求正确,服务器应该能够在选定的员工谷歌日历上创建新的日历条目。注意-在预订期间,用户和员工都不应被要求进行身份验证。

我扫描了谷歌日历v3neneneba API和论坛,仍然没有得到明确的答案,也没有得到简洁的例子——谷歌日历可能出现这种情况吗?有人能帮我回答问题吗?(如果可能的话,分享一个带有适当例子的链接)?

您熟悉添加带有身份验证的事件吗?就像我在这里回答的那样
就像下面的代码一样。如果你是。。。你可以进入下一个级别;)

(你可以在这个页面上下载Google Api Php客户端。这个页面上有一个教程。它适用于Google+,但原理相同)

第一次您总是需要身份验证。这是没有办法的。在下面的示例中,您将在身份验证后返回一个token,其中包括access_tokenrefresh_token。access_token仅在3600秒内有效,用于直接访问。当access_token过期时,您会收到401错误,您可以使用refresh_token(与client_id、client_secret和正确的grant_type一起)来请求新的access_toke。

你可以在这个页面上找到更多信息(在底部"使用刷新令牌")。下面的页面提到了请求这些服务的人数限制。


这是我上次帮助别人做的一些测试代码。它只显示获取身份验证并将token存储在cookie中。它在到期后不会请求新的access_token。

但正如我已经说过的,您需要将token存储在数据库中?当您获得401时,您需要请求一个新的access_token,其中包含client_id、client_secret、correct grant_type和refresh_token。不需要(重新)身份验证。

<?php
error_reporting(E_ALL);
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
session_start();
if ((isset($_SESSION)) && (!empty($_SESSION))) {
   echo "There are cookies<br>";
   echo "<pre>";
   print_r($_SESSION);
   echo "</pre>";
}
$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");
$client->setClientId('###');
$client->setClientSecret('###');
$client->setRedirectUri('http://###/add_calendar.php'); // <- registered web-page
//$client->setDeveloperKey('###'); // <- not always needed
$cal = new Google_CalendarService($client);
if (isset($_GET['logout'])) {
  echo "<br><br><font size=+2>Logging out</font>";
  unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
  echo "<br>I got a code from Google = ".$_GET['code']; // You won't see this if redirected later
  $client->authenticate(); // $_GET['code']
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
  // not strictly necessary to redirect but cleaner for the url in address-bar
  echo "<br>I got the token = ".$_SESSION['token']; // <-- not needed to get here unless location uncommented
}
if (isset($_SESSION['token'])) {
  echo "<br>Getting access";
  $client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()){
  echo "<hr><font size=+1>I have access to your calendar</font>";
  $event = new Google_Event();
  $event->setSummary('=== I ADDED THIS ===');
  $event->setLocation('The Neighbourhood');
  $start = new Google_EventDateTime();
  $start->setDateTime('2013-11-29T10:00:00.000-05:00');
  $event->setStart($start);
  $end = new Google_EventDateTime();
  $end->setDateTime('2013-11-29T10:25:00.000-05:00');
  $event->setEnd($end);
  $createdEvent = $cal->events->insert('###', $event); // <- ### = email of calendar
  echo "<br><font size=+1>Event created</font>";
  echo "<hr><br><font size=+1>Already connected</font> (No need to login)";
} else {
  $authUrl = $client->createAuthUrl();
  print "<hr><br><font size=+2><a href='$authUrl'>Connect Me!</a></font>";
}
$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
echo "<br><br><font size=+2><a href=$url?logout>Logout</a></font>";
?>

编辑:

如果你不想要最初的身份验证,谷歌有它的"服务Acounts",正如你所指出的
(我不知道这些:)

我找到了一些链接,在那里你可以找到在你得到密钥文件后使用的代码。

从谷歌服务帐户编辑谷歌日历事件:403
使用服务帐户访问来自的Google日历事件:{"error"没有谷歌应用程序
Google OAuth 2.0服务帐户-日历API(PHP客户端)
https://groups.google.com/forum/#!主题/谷歌api php客户端/B7KXVQvx1k8
https://groups.google.com/forum/?fromgroups#!主题/谷歌api php客户端/IiwRBKZMZw


编辑#2:是的,成功了。(即使使用我的免费谷歌帐户;)

这是工作代码:

<?
error_reporting(E_ALL);
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
session_start();
echo "Busy<br>";
const CLIENT_ID = 'xxxxxxxxxx.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = 'xxxxxxxxxxx@developer.gserviceaccount.com';
const KEY_FILE = 'xxxxxxxxxxxxxxxxxxx-privatekey.p12';
const CALENDAR_NAME = 'xxxxxxx@gmail.com';
$client = new Google_Client();
$client->setApplicationName("mycal");
if (isset($_SESSION['token'])) {
 $client->setAccessToken($_SESSION['token']);
}
$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
  SERVICE_ACCOUNT_NAME,
  array('https://www.google.com/calendar/feeds/'),
  $key)
);
$client->setClientId(CLIENT_ID);
$service = new Google_CalendarService($client);
$event = new Google_Event();
$event->setSummary('=== I MADE THIS ===');
$event->setLocation('Somewhere else');
$start = new Google_EventDateTime();
$start->setDateTime('2013-11-30T10:00:00.000-02:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2013-11-30T10:25:00.000-02:00');
$event->setEnd($end);
$createdEvent = $service->events->insert(CALENDAR_NAME, $event);
echo "Done<br>";
?>

我需要在https://cloud.google.com/console#/project.我无法使用现有的。在激活"日历API"并使用证书注册新的Web-app后,我只需要与xxxx@developer.gserviceaccount.com以上代码有效
没有身份验证)。

编辑#3:现在它似乎也在默认的"Google+neneneba API项目"中工作