2011-06-09 106 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