You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用Google Calendar API按ID获取事件遇404错误求助

问题原因及解决方案

核心错误:事件ID字段名写错了

你在创建事件时使用了eventId字段指定自定义ID,但Google Calendar API的正确字段名是**id**,不是eventId。API会忽略不符合规范的字段,自动生成随机事件ID,所以你用passengerID查询时找不到对应事件。

修正后的创建事件代码

把事件对象中的'eventId': 'passengerID'替换为'id': 'passengerID':

service = build('calendar', 'v3', credentials=credentials)

now = datetime.datetime.utcnow().isoformat() + 'Z'
events_result = service.events().list(calendarId='primary', timeMin=now,
                                        maxResults=10, singleEvents=True,
                                        orderBy='startTime').execute()
    
events = events_result.get('items', [])
    
event = {
          'summary': 'EVENT TEST',
          'id': 'passengerID',  # 修正为正确的字段名id
          'colorId': '6',
          'location': '800 Howard St., San Francisco, CA 94103',
          'description': "A chance to hear more about Google's developer products.",
          'start': {
            'dateTime': '2025-03-07T09:00:00-08:00',
            'timeZone': 'America/Los_Angeles',
          },
          'end': {
            'dateTime': '2025-03-07T17:00:00-08:00',
            'timeZone': 'America/Los_Angeles',
          },
          'recurrence': [ ],
        
          'attendees': [
            {'email': 'lpage@example.com'},
            {'email': 'sbrin@example.com'},
          ],
          'reminders': {
            'useDefault': False,
            'overrides': [
              {'method': 'email', 'minutes': 24 * 60},
              {'method': 'popup', 'minutes': 10},
            ],
          },
        }
        
event = service.events().insert(calendarId='primary', body=event).execute()
# 打印返回的事件ID,确认是否为passengerID
print("事件ID:", event['id'])

额外注意事项

自定义事件ID需满足以下格式要求:

  • 长度在5到100字符之间
  • 仅可包含字母、数字、下划线(_)、破折号(-)、点号(.)
  • 在同一日历中必须唯一,重复ID会导致创建失败

验证方法

执行修正后的代码后,打印event['id'],如果输出为passengerID,说明自定义ID设置成功,此时再执行service.events().get(calendarId='primary', eventId='passengerID').execute()即可正常获取事件。

内容的提问来源于stack exchange,提问作者Sim81

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 00:17:16