Skip to main content
 首页 » 编程设计

python之谷歌 Cal API Python : Multiple delete with ExecuteBatch failure

2025年05月04日10cmt

我尝试使用适用于 python 的 Google API 删除日历中的所有事件。 如果我一个一个地删除事件,这很好用,但它很长而且不是未优化的,所以我尝试使用 ExecuteBatch 做同样的事情,我用它在日历中插入很多条目(并且在插入之前工作正常)。

def DeleteAllEntryOfCalendar(calendar_client, name_calendar): 
    #Request_feed for doing the delete 
    request_feed = gdata.calendar.CalendarEventFeed() 
    #Get the Uri of the cal we've to delete all entries 
    uri = GetUri(calendar_client, name_calendar) 
    feed = calendar_client.GetAllCalendarsFeed() 
    for a_cal in feed.entry: 
        if a_cal.title.text == name_calendar: 
            calendar = a_cal     
    #For each entry in the cal, do a delete request entry in request_feed 
    query = gdata.calendar.client.CalendarEventQuery() 
    query.max_results = 1000 
    feed = calendar_client.GetCalendarEventFeed(uri=uri, q=query) 
    for an_entry in feed.entry: 
        an_entry.batch_id = gdata.BatchId(text='delete-request') 
        request_feed.AddDelete(entry=an_entry)     
        entry = '' 
 
    # submit the batch request to the server 
    response_feed = calendar_client.ExecuteBatch(request_feed, uri+u'/batch') 

我得到了错误:

Traceback (most recent call last): 
  File "c.py", line 253, in <module> 
    DeleteAllEntryOfCalendar(client, name_calendar) 
  File "c.py", line 187, in DeleteAllEntryOfCalendar 
    response_feed = calendar_client.ExecuteBatch(request_feed, uri+u'/batch') 
  File "/usr/local/lib/python2.6/dist-packages/gdata/calendar/client.py", line 432, in     execute_batch 
    return self.Post(batch_feed, url, desired_class=desired_class) 
  File "/usr/local/lib/python2.6/dist-packages/gdata/client.py", line 681, in post 
    entry.to_string(get_xml_version(self.api_version)), 
AttributeError: 'CalendarEventFeed' object has no attribute 'to_string' 

我重复一遍,当我执行相同的操作以插入事件时,效果很好。 我搜索了很多并尝试了很多可能性,但我无法做到这一点。 如果你有想法...

请您参考如下方法:

使用

gdata.calendar.data.CalendarEventFeed 

gdata python 库中的某些对象有两个版本,您不能将它们混合在一起。