1
對Google日曆進行身份驗證,搜索和添加事件/樣例正如預期的那樣工作,但在400錯誤請求錯誤中刪除結果。爲什麼刪除Google日曆條目/事件時entry.delete()爲400?
該代碼主要是從google's documentation複製。
低於googleUri
是應該刪除的日曆項(由相同的應用程序/用戶創建,標題爲「要刪除的事件」)的鏈接,並且ConfigurationManager.AppSettings
包含認證信息。
調試輸出顯示找到日曆條目,但刪除不成功。
這使用谷歌日曆API v2應該仍然運作,直到10/2014。移動到V3會很好,但據我所知,沒有辦法使用已知的用戶名+密碼進行身份驗證(而是使用需要手動輸入Google憑據(?)的過期令牌)。
Debug.Write ("want to remove: " + googleURI);
// autheticate and get service
CalendarService svc = new CalendarService(ConfigurationManager.AppSettings["GoogleCalendarName"]);
svc.setUserCredentials(ConfigurationManager.AppSettings["GoogleCalendarUsername"], ConfigurationManager.AppSettings["GoogleCalendarPassword"]);
svc.QueryClientLoginToken();
// find the event(s) -- should only be one that can match the googleuri
EventQuery evtquery = new EventQuery(ConfigurationManager.AppSettings["GoogleCalendarPostURI"]);
evtquery.Uri = new Uri(googleURI);
EventFeed evtfeed = svc.Query (evtquery);
//should only be one event in the query
if (evtfeed == null || evtfeed.Entries.Count != 1) {
Debug.Write ("No match or too many matches for " + googleURI); // if this is less than 1, we should panic
return;
}
// we found the right one
EventEntry entry = (EventEntry)(evtfeed.Entries[0]);
Debug.Write ("Title: " + entry.Title.Text);
//hangs here until "The remote server returned an error: (400) Bad Request.
entry.Delete();
輸出是:
[0:] want to remove: https://www.google.com/calendar/feeds/default/private/full/77e0tr0e3b4ctlirug30igeop0
[0:] Title: Event To Delete
我還使用批處理方法來嘗試沒有avial
// https://developers.google.com/google-apps/calendar/v2/developers_guide_dotnet?csw=1#batch
// Create an batch entry to delete an the appointment
EventEntry toDelete = (EventEntry)calfeed.Entries[0];
toDelete.Id = new AtomId(toDelete.EditUri.ToString());
toDelete.BatchData = new GDataBatchEntryData("Deleting Appointment", GDataBatchOperationType.delete);
// add the entry to batch
AtomFeed batchFeed = new AtomFeed(calfeed);
batchFeed.Entries.Add(toDelete);
// run the batch
EventFeed batchResultFeed = (EventFeed)svc.Batch(batchFeed, new Uri(ConfigurationManager.AppSettings["GoogleCalendarPostURI"] ));
// check for succses
Debug.Write (batchResultFeed.Entries [0].BatchData.Status.Code);
if (batchResultFeed.Entries [0].BatchData.Status.Code != 200) {
return;
}