当我使用 Google 日历 API Python 输入新事件时,如何自定义 Google 日历的事件 ID?

How can I custom my Event ID of Google Calendar when I input new event with Google Calendar API Python?

我想用 Python 将我想要的事件 ID 插入到 Google 日历 API, 但我不知道该怎么做,

 event = {
      'summary': acara,
      'location': lokasi,
      'description': deskripsi,
      'start': {
        'dateTime': start_time.strftime("%Y-%m-%dT%H:%M:%S"),
        'timeZone': timezone,
      },
      'end': {
        'dateTime': end_time.strftime("%Y-%m-%dT%H:%M:%S"),
        'timeZone': timezone,
      },


      'attendees': attendees,

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

您可以使用 "Events: insert"。可以看到详情here.

event = service.events().insert(calendarId='primary', body=event).execute()

来源:https://developers.google.com/calendar/v3/reference/events/insert#python

您只需将字段 id 添加到事件资源即可指定您的自定义事件 ID:

 event = {
      'id': 123456
      'summary': acara,
      'location': lokasi,
      'description': deskripsi,
      'start': {
        'dateTime': start_time.strftime("%Y-%m-%dT%H:%M:%S"),
        'timeZone': timezone,
      },
      'end': {
        'dateTime': end_time.strftime("%Y-%m-%dT%H:%M:%S"),
        'timeZone': timezone,
      },


      'attendees': attendees,

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

重要

事件 ID 需要满足 documentation 中指定的特定条件:

When creating new single or recurring events, you can specify their IDs. Provided IDs must follow these rules:

characters allowed in the ID are those used in base32hex encoding, i.e. lowercase letters a-v and digits 0-9, see section 3.1.2 in RFC2938

the length of the ID must be between 5 and 1024 characters

the ID must be unique per calendar

Due to the globally distributed nature of the system, we cannot guarantee that ID collisions will be detected at event creation time. To minimize the risk of collisions we recommend using an established UUID algorithm such as one described in RFC4122.

If you do not specify an ID, it will be automatically generated by the server.

Note that the icalUID and the id are not identical and only one of them should be supplied at event creation time. One difference in their semantics is that in recurring events, all occurrences of one event have different ids while they all share the same icalUIDs.)

自插而已:

Google Calendar Simple API for Python (written by me). It is much simpler and more Pythonic, object-oriented. It handles credentials, JSON formatting, datetimes/timezones, recurrence, etc. for you. Here is the Documentation.

您可以将其用作:

from gcsa.google_calendar import GoogleCalendar
from gcsa.event import Event

calendar = GoogleCalendar('your_email@gmail.com')
event = Event(
    summary=acara,
    event_id=your_id,
    start=start_date,
    end=end_time,
    timezone=timezone,
    attendees=attendees,
    recurrence=Recurrence.rule(freq=DAILY)),
    minutes_before_email_reminder=24 * 60,
    minutes_before_popup_reminder=10
)

calendar.add_event(event)