如何创建不带用户的Google日历条目? [英] How to create Google calendar entry w/o user?

查看:259
本文介绍了如何创建不带用户的Google日历条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Recently I've decided to give users at my site the following functionality: online time slot booking in calendars of my employees. All employees have Google accounts linked to our domain in Google Apps (free edition). Users make booking in some front-end (e.g. not directly in employees Google calendars), then request is processed at our PHP server and if it is correct, server should be able to create new calendar entry in selected employee Google calendar. Note - neither user, nor employee should not be asked for authentication during booking.

我已经扫描了Google Calendar v3 API和论坛,但仍然没有得到明确答案和简洁示例-Google日历是否可能出现这种情况?有人可以帮助我回答问题吗?(如果可能,请提供带有适当示例的链接)?

I've scanned Google calendar v3 API and forums and still didn't get neither clear answer not concise examples - is such scenario possible with Google calendar? Can someone help me to answer the Q (and if possible - share a link with proper example)?

推荐答案

您是否熟悉添加带有身份验证的事件 ?就像我在此处回答. 就像下面的代码一样.如果您是...,则可以进入下一个级别;)

Are you familiar with adding an event with authentication? Like i answered here.
And like in the code below. If you are... you can go to the next level ;)

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

(You can download the Google Api Php-client on this page. On this page there is a tutorial. It's for Google+ but the principle is the same)

您第一次始终需要身份验证.没有办法解决.在下面的示例中,您在身份验证后返回了token,其中包括 access_token refresh_token . access_token仅有效3600秒,用于直接访问.当access_token过期时,您会收到401错误,并且可以使用refresh_token(以及client_id,client_secret和正确的grant_type一起)来请求新的access_token.

The first time you would always need authentication. There is no way around that. In the example below you get a token back after authentication, which includes an access_token and a refresh_token. The access_token is only valid for 3600 seconds and is used for direct access. When the access_token is expired you get a 401 error and you can use the refresh_token (together with client_id, client_secret and the correct grant_type) to request a new access_token.

您可以在此页面(位于使用刷新令牌").底部的页面上提到了请求数量的限制.

You can find some more information on this page (at the bottom "Using a Refresh Token"). There are some limits in numbers to request these, mentioned on that page at the bottom.

这是我上次帮助某人时编写的一些测试代码.它仅显示获得身份验证并将token存储在cookie中.过期后,它不会再请求新的access_token.

This is some testing code i made the last time i helped someone with this. It only shows getting the authentication and storing the token in a cookie. It does not request a new access_token after it expires.

但是正如我已经说过的那样,您需要存储token(在数据库中?),当您收到401时,您需要使用client_id,client_secret,正确的grant_type和refresh_token请求一个新的access_token.不需要(重新)认证.

But as i already said you need to store the token (in a database?) and when you get a 401 you need to request a new access_token with the client_id, client_secret, correct grant_type and refresh_token. There is no (re-)authentication necessary for that.

<?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>";

?>


如您所指出的,如果您不希望初始身份验证,则Google会提供其服务帐户".
(我不知道这些:)

If you don't want the initial authentication Google has its "Service Acounts", as you pointed out.
(I didn't know about these :)

我挖了一些链接,在您找到密钥文件后,您可以在其中找到要使用的代码.

I dug up some links where you can find code to use after you got the key-file.

编辑Google服务帐户中的Google日历事件:403
使用服务帐户访问Google日历事件:{ 错误" :"access_denied"; }.没有Google应用
Google OAuth 2.0服务帐户-日历API(PHP客户端)
https://groups.google.com/forum/#!topic/google-api-php-client/B7KXVQvx1k8
https://groups.google.com/论坛/?fromgroups#!topic/google-api-php-client/IiwRBKZMZxw

Edit Google calendar events from Google service account: 403
Access Google calendar events from with service account: { "error" : "access_denied" }. No google apps
Google OAuth 2.0 Service Account - Calendar API (PHP Client)
https://groups.google.com/forum/#!topic/google-api-php-client/B7KXVQvx1k8
https://groups.google.com/forum/?fromgroups#!topic/google-api-php-client/IiwRBKZMZxw

编辑#2 :是的,可以使用了. (即使使用我的免费Google帐户;)

Edit #2: YES, got it working. (even with my free Google account ;)

这是工作代码:

<?
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/上创建一个新项目".控制台#/项目.我不能使用现有的.激活日历API"并使用证书注册新的Web应用程序后,我只需要与xxxx@developer.gserviceaccount.com共享我的日历,以上代码即可工作
(未经身份验证).

I needed to make a "new project" on https://cloud.google.com/console#/project. I couldn't use the existing one. After activating "Calendar API" and registering a new Web-app with Certificate i only needed to share my calendar with the xxxx@developer.gserviceaccount.com and above code worked
(without authentication).

编辑#3:现在,它似乎也可以在默认的"Google+ API项目"中正常工作

这篇关于如何创建不带用户的Google日历条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆