温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - google calendar api not adding event and not throwing an error

python - 谷歌日历API不添加事件,也不抛出错误

发布于 2020-03-27 16:15:09

我的日历

代码

链接指向的日历

I'm trying to follow google's example of adding a event and originally it threw errors because I formatted something wrong but now it's saying the event is being made and linking me to it but it never actually makes the event. The 3rd picture I attached is the result and it has a little popup that says error adding event, the weird thing is that none of my events at all show up on the calendar even though I'm logged into the same account in both cases and only have one calendar. The event never gets added to my calendar I've tried running it multiple times even using different data in the event and same result, it says it adds it but never does. I'm lost any help is greatly appreciated please feel free to ask for more info if you need!

edit: here's my event creation function

def createEvent(summary, start_time, end_time, *args, description='', location='', timeZone='America/New_York'):

    credentials = get_credentials()
    service = discovery.build('calendar', 'v3', credentials=credentials)

    event = {
      'summary': summary,
      'location': location,
      'description': description,
      'start': {
        'dateTime': start_time,
        'timeZone': timeZone,
      },
      'end': {
        'dateTime': end_time,
        'timeZone': timeZone,
      },

      'reminders': {
        'useDefault': False,
        'overrides': [
          # {'method': 'email', 'minutes': 24 * 60},
          {'method': 'popup', 'minutes': 10},
        ],
      },
    }

    for arg in args:
        event[arg[0]] = arg[1]

    event = service.events().insert(calendarId='primary', body=event).execute()
    print ('Event created: %s' % (event.get('htmlLink')))

编辑2:这是通过传入的信息示例调用函数的方式

googEvent = ['CSC 385 hw', '20-1-31T22:59:59', '20-1-31T23:59:59', 'EC Mylavarapu']

createEvent(googEvent[0], googEvent[1], googEvent[2], description=googEvent[3])

查看更多

查看更多

提问者
mizuprogrammer
被浏览
35
Jacques-Guzel Heron 2020-01-31 17:09

在研究了您的代码之后,我发现您已经接近修复它。您只需要将日期格式强制为ISO 8601为此,我使用了以下Python方法:

import datetime
googEvent = ['CSC 385 hw', datetime.datetime.strptime("31/01/2020 22:59:59",
  "%d/%m/%Y %H:%M:%S").isoformat(), datetime.datetime.strptime(
  "31/01/2020 23:59:59", "%d/%m/%Y %H:%M:%S").isoformat(), 'EC Mylavarapu']
createEvent(googEvent[0], googEvent[1], googEvent[2], description = googEvent[3])

这只是这样做的一种方式。每个日期都首先使用可读字符串创建,然后使用strptime()转换为ISO 8601 isoformat()请给我回覆,如果您需要进一步的协助。