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

时间:2018-08-14 15:21:05

标签: python google-calendar-api google-api-python-client

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

使用我的代码,注释掉“删除事件”代码并运行,该代码将插入一个事件,然后列出该事件。我还要检查以确保可以在用户界面中看到它。然后,我从创建的事件中获取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,则该事件将被毫无问题地删除。想法?

1 个答案:

答案 0 :(得分:0)

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

  

每个集合都有API定义的方法列表。调用集合的方法将返回一个HttpRequest对象。如果给定的API集合具有一个名为list的方法,该方法带有一个名为cents的参数,则可以为该方法创建一个请求对象,如下所示:

     

request = collection.list(cents=5)

     

创建请求实际上并不调用API。要执行请求并获得响应,请调用execute()函数:

     

response = request.execute()

     

或者,您可以将先前的步骤合并在一行上:

     

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

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

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