Google Calendar API Redirect Exception issue
I'm running into a peculiar problem when accessing Google Calendar -- sometimes it generates a 'redirect exception' but most of the times it works just fine.
I have no idea how to redirect the call, and from what I read after some searching it should be handled by Google's API itself invisible for the calling function (but obviously isn't.)
The code accesses the Google calendar, gets a list of all the secondary calendars and the number of events each calendar has.
API version 1.8.0.0 (http://code.google.com/p/google-gdata/downloads/list)
Error message:
Execution resulted in a redirect from https://www.google.com/calendar/feeds/tlf1juohv473hh0jhm046do5g9@group.calendar.google.com/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.开发者_JAVA技巧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()
Code (it just collects the calendars, titles and eventcount in a collection):
public bool GetSecondaryCalendars(out List<CalendarData> calendarlist)
{
Console.WriteLine( "Starting calendarlist retrieval..." );
calendarlist = new List<CalendarData>();
CalendarService myService = new CalendarService( "Calendar-application" );
myService.setUserCredentials( "MyAccount@google.com", "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( "MyAccount@google.com" ) ))
{
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; }
}
Any suggestions are more than welcome.
From my pov you are working over the Internet - thus you should plan to try everything at least twice before giving up ;-)
Thus I would do one retry and then raise an actual exception visible to the end user.
精彩评论