google api .net客户端v3获取免费的忙碌信息

时间:2014-03-14 23:15:59

标签: google-calendar-api google-api-dotnet-client

我正在尝试从Google日历查询免费的忙碌数据。我只提供开始日期/时间和结束日期/时间。我想知道的是这个时间框架是否可用。当我在查询下面运行时,我得到" responseOBJ"响应对象似乎并不包括我需要的东西。响应对象仅包含开始和结束时间。它不包含诸如" IsBusy"等标志。 " IsAvailable"

https://developers.google.com/google-apps/calendar/v3/reference/freebusy/query

        #region Free_busy_request_NOT_WORKING
        FreeBusyRequest requestobj = new FreeBusyRequest();

        FreeBusyRequestItem c = new FreeBusyRequestItem();
        c.Id = "calendarresource@domain.com";
        requestobj.Items = new List<FreeBusyRequestItem>();
        requestobj.Items.Add(c);

        requestobj.TimeMin = DateTime.Now.AddDays(1);
        requestobj.TimeMax = DateTime.Now.AddDays(2);


        FreebusyResource.QueryRequest TestRequest = calendarService.Freebusy.Query(requestobj);
      //  var TestRequest = calendarService.Freebusy.
       // FreeBusyResponse responseOBJ = TestRequest.Execute();
        var responseOBJ = TestRequest.Execute();
        #endregion

2 个答案:

答案 0 :(得分:2)

Calendar API只会在响应中提供有序繁忙的块,从不提供可用的块。忙碌之外的一切都可以。你在日历上至少有一个活动吗? 在时间窗口中使用给定的ID? 此外,您使用的帐户至少需要对资源进行忙闲访问才能检索可用性。

答案 1 :(得分:0)

我知道这个问题很旧,但是我认为看一个例子会有所帮助。您实际上需要从响应中获取“忙碌”信息。下面是我自己的代码片段(减去调用),其中包含如何处理响应。您将需要使用c.Id作为搜索响应的键:

FreebusyResource.QueryRequest testRequest = service.Freebusy.Query(busyRequest);
var responseObject = testRequest.Execute();

bool checkBusy;
bool containsKey;
if (responseObject.Calendars.ContainsKey("**INSERT YOUR KEY HERE**"))
{
    containsKey = true;
    if (containsKey)
    {
        //Had to deconstruct API response by WriteLine(). Busy returns a count of 1, while being free returns a count of 0. 
        //These are properties of a dictionary and a List of the responseObject (dictionary returned by API POST).
        if (responseObject.Calendars["**YOUR KEY HERE**"].Busy.Count == 0)
        {
            checkBusy = false;
            //WriteLine(checkBusy);
        }
        else
        {
            checkBusy = true;
            //WriteLine(checkBusy);
        }

        if (checkBusy == true)
        {
            var busyStart = responseObject.Calendars["**YOUR KEY HERE**"].Busy[0].Start;
            var busyEnd = responseObject.Calendars["**YOUR KEY HERE**].Busy[0].End;
            //WriteLine(busyStart);
            //WriteLine(busyEnd);
            //Read();

            string isBusyString = "Between " + busyStart + " and " + busyEnd + " your trainer is busy";
            richTextBox1.Text = isBusyString;
        }
        else
        {
            string isFreeString = "Between " + startDate + " and " + endDate + " your trainer is free";
            richTextBox1.Text += isFreeString;
        }
    }
    else
    {
        richTextBox1.Clear();
        MessageBox.Show("CalendarAPIv3 has failed, please contact support\nregarding missing <key>", "ERROR!");
    }
}

我的建议是通过将响应写到控制台来分解响应。然后,您可以“解构”它们。这就是我能够找出响应中的“位置”的方式。如上所述,您只会收到有关busyBlocks的信息。我使用客户搜索选择的日期和时间来显示“空闲”时间。

编辑:

在尝试TryGetValue或使用键值对进行搜索之前,您需要检查密钥是否存在。