2011-06-09 137 views
0

访问Google日历时,我遇到了一个特殊问题 - 有时它会生成一个“重定向异常”,但大多数情况下它工作得很好。Google Calendar API重定向异常问题

我不知道如何重定向的号召,从我读经过一番搜索,应通过谷歌的API本身的调用函数无形处理(但显然不是。)

访问的代码Google日历获取所有辅助日历的列表以及每个日历具有的事件数量。

API版本1.8.0.0(http://code.google.com/p/google-gdata/downloads/list

错误消息:

 Execution resulted in a redirect from https://www.google.com/calendar/feeds/[email protected]/private/full?gsessionid=MGsId0ULhyO_DkKlGa9DHw 
     at Google.GData.Client.GDataRequest.Execute() 
     at Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter) 
     at Google.GData.Client.GDataGAuthRequest.Execute() 
     at Google.GData.Client.Service.Query(Uri queryUri, DateTime ifModifiedSince, String etag, Int64& contentLength) 
     at Google.GData.Client.Service.Query(Uri queryUri, DateTime ifModifiedSince) 
     at Google.GData.Client.Service.Query(FeedQuery feedQuery) 
     at GoogleDataTool.CalendarActions.GetSecondaryCalendars(Boolean IncludeEventCount, List`1& calendarlist) in D:\GoogleDataTool\classes\CalendarActions.cs:line 65 

     Response.Message: "Stream was not readable." 
     at Google.GData.Client.GDataRequest.Execute() 
     at Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter) 
     at Google.GData.Client.GDataGAuthRequest.Execute() 
     at Google.GData.Client.Service.Query(Uri queryUri, DateTime ifModifiedSince, String etag, Int64& contentLength) 
     at Google.GData.Client.Service.Query(Uri queryUri, DateTime ifModifiedSince) 
     at Google.GData.Client.Service.Query(FeedQuery feedQuery) 
     at GoogleDataTool.CalendarActions.GetSecondaryCalendars(Boolean IncludeEventCount, List`1& calendarlist) in D:\GoogleDataTool\classes\CalendarActions.cs:line 84 
     at GoogleDataTool.Program.DoActions() in D:\GoogleDataTool\Program.cs:line 34 
     at GoogleDataTool.Program.Main(String[] args) in D:\GoogleDataTool\Program.cs:line 14 
     at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 

码(它只是收集日历,标题和EVENTCOUNT一个集合中):

 public bool GetSecondaryCalendars(out List<CalendarData> calendarlist) 
    { 
     Console.WriteLine("Starting calendarlist retrieval..."); 

     calendarlist = new List<CalendarData>(); 

     CalendarService myService = new CalendarService("Calendar-application"); 
     myService.setUserCredentials("[email protected]", "MyPassword"); 

     CalendarQuery cQuery = new CalendarQuery(); 
     cQuery.Uri = new Uri("https://www.google.com/calendar/feeds/default/allcalendars/full"); 

     try 
     { 
      CalendarFeed cFeed = (CalendarFeed)myService.Query(cQuery); 

      foreach (CalendarEntry entry in cFeed.Entries) 
      { 
       // We don't want the primary calendar; just the secondary ones 
       if (!entry.Id.Uri.ToString().Contains(HttpUtility.UrlEncode("[email protected]"))) 
       { 
        String calendarid = entry.Id.Uri.ToString().Substring(entry.Id.Uri.ToString().LastIndexOf("/") + 1); 
        calendarlist.Add(new CalendarData() 
        { 
         Text = entry.Title.Text, 
         Id = calendarid 
        }); 
       } 

       // Grab each calendar to count the number of events it has (not pretty, but I wish I could just ask the cFeed entries...) 
       foreach (CalendarData calendar in calendarlist) 
       { 
        FeedQuery query = new FeedQuery(); 

        // Create the query object: 
        query.Uri = new Uri(string.Format("https://www.google.com/calendar/feeds/{0}/private/full", calendar.Id)); 

        // Tell the service to query: 
        AtomFeed calFeed = myService.Query(query); 
        calendar.EventCount = calFeed.Entries.Count; 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
      return false; 
     } 
     return true; 
    } 

    public class CalendarData 
    { 
     public string Id { get; set; } 
     public string Text { get; set; } 
     public int EventCount { get; set; } 
    } 

任何建议是非常值得欢迎的。

回答

0

从我的POV您正在通过互联网 - 因此你应该计划尝试一切至少两次放弃;-)
之前,因此,我会做一个重试,然后引发实际的异常给最终用户可见。

+0

这是一个好主意,也可能是我必须去的方式;但它补偿了谷歌的API应该处理的事情。 :) – MartijnK 2011-06-10 08:26:14

+0

我已经移动了函数返回日历的完整列表,以及检索给定日历的所有事件到一个单独的函数,试图放弃之前是六次。这似乎使它更可靠。 不是很漂亮,但它的作品。 – MartijnK 2011-06-10 09:11:47