我一直在使用 Google Calendar API,但遇到了一些问题。当我在下面调用它来删除日历事件时,它在第一次传递时工作正常,通常是第二次传递。但是,在我第二次或第三次调用此方法时,我得到一个(401) Unauthorized error。它每次都使用相同的凭据。如果我得到异常,我可以重置catch 中的凭据,它工作正常。我宁愿不必这样做。有任何想法吗?
CalendarService myService = new CalendarService("mycompany-myapp-1");
myService.setUserCredentials("jo@username.com", "password");
// set the query for the event
EventQuery myQuery = new EventQuery(("http://www.google.com/calendar/feeds/jo@username.com/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("jo@username.com", "password");
myResultsFeed = myService.Query(myQuery);
}
if (myResultsFeed != null && myResultsFeed.Entries.Count > 0)
{
AtomEntry firstMatchEntry = myResultsFeed.Entries[0];
firstMatchEntry.Delete();
}