Skip to main content
 首页 » 编程设计

python之使用 Google 的 Python 客户端库删除日历事件

2024年12月31日8linjiqin

这似乎是一件很简单的事情,但我似乎无法使用该库删除 Google 日历中的事件。

在我的代码中,我注释掉“删除事件”代码并运行,这会插入一个事件,然后列出该事件。我还检查以确保我可以在 UI 中看到它。然后,我从创建的事件中获取 id,然后将其作为 eventId 放入“删除事件”调用中。然后我把删除代码取消注释,把插入代码注释掉,再运行。

这种方法似乎不起作用:我仍然在 events().list 输出中看到该事件,而且我仍然在 UI 中看到它。

# Delete Event 
resp = service.events().delete(calendarId='primary', eventId='9c9ppp94isp15103t54mtjss8s') 
pprint(vars(resp)) 
print('Event Deleted') 
 
 
# Inserts Event 
GMT_OFF = '-07:00'    # PDT/MST/GMT-7 
EVENT = { 
    'summary': 'Test Event2', 
    'start':   {'dateTime': '2018-09-15T19:00:00%s' % GMT_OFF}, 
    'end':     {'dateTime': '2018-09-15T22:00:00%s' % GMT_OFF}, 
} 
 
e = service.events().insert(calendarId='primary', sendNotifications=True, body=EVENT).execute() 
 
print('''*** %r event added: 
Start: %s 
End:   %s''' % (e['summary'].encode('utf-8'), 
    e['start']['dateTime'], e['end']['dateTime'])) 
 
 
 
# Read Events 
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time 
print('Getting the upcoming 10 events') 
events_result = service.events().list(calendarId='primary', timeMin=now, 
                                    maxResults=10, singleEvents=True, 
                                    orderBy='startTime').execute() 
events = events_result.get('items', []) 
 
if not events: 
    print('No upcoming events found.') 
for event in events: 
    start = event['start'].get('dateTime', event['start'].get('date')) 
    print(start, event['id'], event['summary']) 

当我打印出删除响应 (pprint(vars(resp))) 时,它看起来像这样:

{'_in_error_state': False, 
 '_rand': <built-in method random of Random object at 0x7f9ac007e420>, 
 '_sleep': <built-in function sleep>, 
 'body': None, 
 'body_size': 0, 
 'headers': {'accept': '*/*', 
         'accept-encoding': 'gzip, deflate', 
         'user-agent': 'google-api-python-client/1.7.4 (gzip)'}, 
 'http': <httplib2.Http object at 0x1051cd250>, 
 'method': u'DELETE', 
 'methodId': u'calendar.events.delete', 
 'postproc': <bound method RawModel.response of  
 <googleapiclient.model.RawModel object at 0x1052e9290>>, 
 'response_callbacks': [], 
 'resumable': None, 
 'resumable_progress': 0, 
 'resumable_uri': None, 
 'uri': u'https://www.googleapis.com/calendar/v3/calendars/primary/events/9c9ppp94isp15103t54mtjss8s?'} 

如果我从上面的删除响应中复制 uri 并使用 Postman,事件将被毫无问题地删除。想法?

请您参考如下方法:

根据 Python API 客户端文档,calling an API endpoint does not submit the call, but rather constructs the relevant HttpRequest object:

Every collection has a list of methods defined by the API. Calling a collection's method returns an HttpRequest object. If the given API collection has a method named list that takes an argument called cents, you create a request object for that method like this:

request = collection.list(cents=5)

Creating a request does not actually call the API. To execute the request and get a response, call the execute() function:

response = request.execute()

Alternatively, you can combine previous steps on a single line:

response = service.stamps().list(cents=5).execute()

因此,对于您的代码,您需要将 .execute() 添加到删除行。例如:

kwargs = {calendarId: 'primary', 
          eventId: '9c9ppp94isp15103t54mtjss8s', 
          sendNotifications: False} 
rq = service.events().delete(**kwargs) 
resp = rq.execute()