Google Calendar API:选择/创建日历? [英] Google calendar API : Selecting/Creating calendars?

查看:125
本文介绍了Google Calendar API:选择/创建日历?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我希望我们的工作日历能够在日程安排经理发布/更新日程时自动与每个员工的Google日历同步.用户最初会使用AuthSub令牌选择加入,但此后应该是自动的.为了避免与他们安排的其他事件发生冲突,我想创建一个名为"Work App"或其他相当独特的新日历.然后进行更新,只需在创建新事件之前删除该范围内的任何事件即可.所以我有一些问题...

So I want our work calendar to automatically sync with each employee's Google Calendar when the scheduling manager posts/updates the schedules. Users would initially opt-in using the AuthSub token, but after that it should be automatic. In order to avoid conflicts with other events they have scheduled, I want to create a new calendar called "Work App" or something else fairly unique. Then for updates, it would simply delete any events from that range before creating the new events. So I have some questions...

  1. 如何使用API​​选择想要的特定日历? (顺便说一下,所有这些都在PHP中使用Zend Framework).我在哪里可以查询日历列表,但是文档没有显示如何添加到该特定日历.

  1. How do I select the specific calendar I want using the API? (this all in PHP, by the way, using the Zend Framework). I see where I can query for the list of calendars, but the documentation doesn't show how to add to that specific calendar.

我可以创建日历吗?我是否只需要让选择加入的用户创建这样的日历?如果是这样,我是否可以为他们生成一个URL,以创建一个类似于添加事件的URL? (有什么可以使事情变得简单和一致的,对吧?)

Can I create calendars? Do I need to just have the users who opt in create such a calendar? If so, is there a URL I could generate for them to create one like there is for adding events? (Anything to make things simple and consistent, right?)

很显然,我不想用户存储其凭据,但是我也不想为每个更新请求访问权限.我是否需要OAuth令牌才能进行持久访问?我知道该URL令牌是一次性使用的,但是我可以存储会话令牌并在一周后重用吗?

I don't want users to store their credentials, obviously, but I also don't want to request access for each update. Do I need an OAuth token in order to have persistent access? I know that the URL token is one time use, but can I store session token and reuse it a week later?

除了查询每个事件之外,还有其他方法可以避免重复吗?昨晚我正在测试,现在我有7个实例执行了相同的50个事件.我不想在每次添加之前进行检查来增加服务器时间.

Is there a way other than querying for each event to avoid duplicates? I was testing this last night and I now have 7 instances of the same 50 events. I don't want to add to the server time with checking before each add.

最后,有一种方法可以添加多个事件,甚至可以简单地将ics文件推送到日历中.现在,我让脚本执行SQL查询,并在循环遍历结果时添加每个事件.这似乎比预期要花费更长的时间,因此仅构建ics文件或将所有事件添加到一个对象并将它们全部一次添加会更好. 谢谢!

Finally, is there a way to add several events, or even to simply push the ics file to the calendar. Right now I have the script do a SQL query and add each event as it loops through the results. It seems to take much longer than expected, so just building either the ics file or adding all of the events to one object and adding them all at once would be better. Thanks!

推荐答案

好吧,由于没有人回答,我决定开始浏览有关Google Calendar API的非PHP文档,特别是.NET方面的内容.原始协议中的位.而且你不知道吗...

Well since no one answered, I decided to start poking around at the non-PHP documentation for the Google Calendar API, specifically at the .NET stuff and just a bit in the raw protocol. And wouldn't you know it...

如果您转到.NET文档,它会提到很酷的 功能,特别是如何为经过身份验证的用户创建新的非主要日历,以及如何向非已注册用户添加事件-主日历.

If you go to the .NET documentation it mentions cool new features, specifically how to create new non-primary calendars for authenticated users and how to add events to non-primary calendars.

当然,该文档在PHP区域中没有显示,并且似乎没有一对一的关联.为了创建新日历,我首先尝试了一些显而易见的工作,然后尝试了一些不太明显但有效的工作.我以为我会分享无线电沉默的原因,那就是没人知道答案,但肯定愿意.

Of course, this documentation shows up nowhere in the PHP area, and there does not seem to be a one-to-one correlation. For the creating of the new calendar, I tried some obvious stuff first, then, going for broke, tried something not-so-obvious that worked. I thought I'd share in case the reason for the radio silence was that no one knew the answer but sure would like to.

有两个键:

  1. 您必须使用相同的方法添加日历事件,即insertEvent()

您必须在方法中设置发布网址,否则将使用默认的供稿网址.

You have to set the post URL in the method, which otherwise goes to the default feed URL.

此示例检查以查看应用日历是否已经存在,如果不存在,请创建它:

This example checks to see if the App Calendar already exists and if not, creates it:

 //Standard creation of the HTTP client
 $gdataCal = new Zend_Gdata_Calendar($client);

 //Get list of existing calendars
 $calFeed = $gdataCal->getCalendarListFeed();

 //Set this to true by default, only gets set to false if calendar is found
 $noAppCal = true;

 //Loop through calendars and check name which is ->title->text
 foreach ($calFeed as $calendar) {
  if($calendar -> title -> text == "App Calendar") {
   $noAppCal = false;
  }
 }

 //If name not found, create the calendar
 if($noAppCal) {

  // I actually had to guess this method based on Google API's "magic" factory
  $appCal = $gdataCal -> newListEntry();
  // I only set the title, other options like color are available.
  $appCal -> title = $gdataCal-> newTitle("App Calendar"); 

  //This is the right URL to post to for new calendars...
  //Notice that the user's info is nowhere in there
  $own_cal = "http://www.google.com/calendar/feeds/default/owncalendars/full";

  //And here's the payoff. 
  //Use the insertEvent method, set the second optional var to the right URL
  $gdataCal->insertEvent($appCal, $own_cal);
 }

就在那里.下一个目标是将事件插入该日历,而不是默认日历.

And there you have it. The next goal is to insert events to that calendar, not to the default calendar.

您可能会猜到的最简单的部分是,您需要再次设置该可选URL,例如:insertEvent($newEvent, $calURL),棘手的部分是获取日历的URL.与拥有的日历"路径不同,特定的日历不仅在其中包含用户指定的信息,而且在其中也具有某种哈希查找ID.

The easy part that you can probably guess is that you need to set that optional URL again, like such : insertEvent($newEvent, $calURL), the tricky part is getting the calendar's URL. Unlike the "owned calendars" path, specific calendars not only have user-specifc info in it, they have some kind of hash-lookin' ID in there, too.

代码如下:

 //Set up  that loop again to find the new calendar:
 $calFeed = $gdataCal->getCalendarListFeed();
 foreach ($calFeed as $calendar) {
  if($calendar->title->text == "App Calendar")
   //This is the money, you need to use '->content-src'
   //Anything else and you have to manipulate it to get it right. 
   $appCalUrl = $calendar->content->src;
 }

 //.......... Some Assumed MySQL query and results .............

      while ($event = $result->fetch_assoc()) {
   $title = $event['description'];

   //Quick heads up
   //This is a handy way of getting the date/time in one expression.
   $eventStart = date('Y-m-d\TH:i:sP', strtotime($event['start']));
   $eventEnd = date('Y-m-d\TH:i:sP', strtotime($event['end']));

   $newEvent = $gdataCal->newEventEntry();
   $newEvent->title = $gdataCal->newTitle($title);
   $when = $gdataCal->newWhen();
   $when->startTime = $eventStart;
   $when->endTime = $eventEnd;

   $newEvent->when = array($when);

   //And at last, the insert is set to the calendar URL found above
   $createdEvent = $gdataCal->insertEvent($newEvent, $appCalUrl);
  }
  echo "<p>".$result->num_rows." added to your Google calendar.</p>";

感谢阅读我的问题并对此进行了任何思考的任何人.如果有人知道一种加强上述代码的方法(也许我不需要两个循环?),我很想获得反馈.

Thanks to anyone who read my question and did any thinking on it. If anyone knows of a way to tighten the above code up (maybe I don't need two loops?) I'd love to get feedback.

这篇关于Google Calendar API:选择/创建日历?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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