用于asp.net的gmail日历分页

时间:2014-12-10 08:40:39

标签: asp.net google-calendar-api

任何人都可以告诉我如何在google日历中实现事件列表的分页, 这是我到目前为止所做的事情列表,,, 在api我使用过滤器选项作为NUmberToRetrieve和StartIndex,但是在这里我看到了这样的过滤器,我只看到了maxResult和PageToken。我无法弄清楚页面令牌是如何工作的, 任何帮助我都非常感激, 提前谢谢你

       string serviceAccountEmail = "{someTExt}@developer.gserviceaccount.com";
        string filepath = HttpContext.Current.Server.MapPath("~/App_Data/Key.p12");
        var certificate = new X509Certificate2(filepath, "notasecret",
        X509KeyStorageFlags.Exportable);

        var credential = new ServiceAccountCredential(new
        ServiceAccountCredential.Initializer(serviceAccountEmail)
        {
            Scopes =
                new[] { CalendarService.Scope.Calendar }
        }.FromCertificate
        (certificate));

        BaseClientService.Initializer initializer = new
        BaseClientService.Initializer();
        initializer.HttpClientInitializer = credential;
        initializer.ApplicationName = "Google Calendar Sample";
        //  CalendarService calservice = new CalendarService(initializer);
        CalendarService _calservice;
        string CalendarId = "yourcalenderId";


        _calservice = new CalendarService(initializer);

        var calendarEvents = new List<GenericEvent>();
        try
        {
            EventsResource.ListRequest req = _calservice.Events.List(CalendarId);
            req.TimeMin = new DateTime(2014,12,1); //Convert.ToDateTime(startDate);
            req.TimeMax = new DateTime(2014, 12, 15);
            req.SingleEvents = true;
           // req.MaxResults = numberToRetrive;

            req.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;


            Events events = req.Execute();

            foreach (Event eventdata in events.Items)
            {

                if (eventdata.Start.DateTime != null)
                {
                    if (eventdata.End.DateTime != null)
                    {
                        var item = new GenericEvent
                        {

                            Title = eventdata.Summary,
                            Contents = eventdata.Description,
                            Location = eventdata.Location,
                            StartTime = eventdata.Start.DateTime.Value,
                            EndTime = eventdata.End.DateTime.Value

                        };

                        calendarEvents.Add(item);
                    }
                }
            }

1 个答案:

答案 0 :(得分:0)

它有点棘手基本上你需要设置request.PageToken = result.NextPageToken并再次发送请求。

这些是我使用的方法。您可以在底部找到指向完整班级的链接。

public static Events list(CalendarService service, string id  ,OptionalValues optionalValues)
        {

            var request = service.Events.List(id);

            if (optionalValues == null)
            {
                request.MaxResults = 100;
            }
            else
            {
                request.MaxResults = optionalValues.MaxResults;
                request.ShowDeleted = optionalValues.ShowDeleted;
            }

            return ProcessResults(request);
        }

 // Just loops though getting all the rows.  
        private static Events ProcessResults(EventsResource.ListRequest request)
        {
            try
            {
                Events result = request.Execute();
                List<Event> allRows = new List<Event>();

                //// Loop through until we arrive at an empty page
                while (result.Items != null)
                {
                    //Add the rows to the final list
                    allRows.AddRange(result.Items);

                    // We will know we are on the last page when the next page token is
                    // null.
                    // If this is the case, break.
                    if (result.NextPageToken == null)
                    {
                        break;
                    }
                    // Prepare the next page of results
                    request.PageToken = result.NextPageToken;

                    // Execute and process the next page request
                    result = request.Execute();

                }
                Events allData = result;
                allData.Items = (List<Event>)allRows;
                return allData;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }

代码摘自DaimtoEventHelper.cs