Ive been playing around with the Google Calendar API and am getting stuck on something. When i call this below to delete a calendar event it works fine on the first pass and usually the second. However, around the 2nd or 3rd time I call this method I get a (401) Unauthorized error. It uses the same credentials every time. If I get the exception, I can reset the credentials in the catch and it works fine. I would prefer not to have to do this. Any ideas?
CalendarService myService = new CalendarService("mycompany-myapp-1");
myService.setUserCredentials("[email protected]", "password");
// set the query for the event
EventQuery myQuery = new EventQuery(("http://www.google.com/calendar/feeds/[email protected]/private/full"));
myQuery.Query = "Cut the grass";
myQuery.StartTime = DateTime.Now;
myQuery.EndTime = DateTime.Now.AddDays(1);
// find the event
EventFeed myResultsFeed = null;
try
{
// execute the query to find the event
myResultsFeed = myService.Query(myQuery);
}
catch (Exception ex)
{
// this is where i get the unauthorized exception
// if i reset the credentials here it works fine
myService.setUserCredentials("[email protected]", "password");
myResultsFeed = myService.Query(myQuery);
}
if (myResultsFeed != null && myResultsFeed.Entries.Count > 0)
{
AtomEntry firstMatchEntry = myResultsFeed.Entries[0];
firstMatchEntry.Delete();
}