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

如何使用Delphi REST控件向Google Calendar插入事件?

使用Delphi REST控件向Google Calendar插入事件

看起来你已经开始搭建插入Google Calendar事件的核心JSON结构了,不过代码好像没写完~先把你目前的代码整理出来:

procedure TForm1.TestGoogleRestParams;
var
  i: Integer;
  jsonObjEventResource,jsonObjStart,jsonObjEnd: TJSONObject;
begin
  try
    jsonObjEventResource:=TJSONObject.Create();
    jsonObjStart:=TJSONObject.Create();
    jsonObjEnd:=TJSONObject.Create();

    jsonObjEventResource.AddPair(TJSONPair.Create('summary','test'));
    jsonObjEventResource.AddPair(TJSONPair.Create('description','Testing'));
    // 这里代码没写完,应该继续处理开始/结束时间的JSON对象
  finally
    // 别忘了释放JSON对象,避免内存泄漏
    jsonObjEventResource.Free;
    jsonObjStart.Free;
    jsonObjEnd.Free;
  end;
end;

给你几个关键的补充建议,帮你完成这个功能:

  • 必填的时间字段:Google Calendar API要求事件必须包含start和end对象,你需要给jsonObjStart和jsonObjEnd添加时间参数。如果是全天事件用date字段(格式YYYY-MM-DD),如果是具体时段用dateTime字段(格式YYYY-MM-DDTHH:MM:SSZ,注意是UTC时间),比如:
    jsonObjStart.AddPair('dateTime', '2024-06-15T10:00:00Z');
    jsonObjStart.AddPair('timeZone', 'Asia/Shanghai'); // 可选,指定时区
    jsonObjEnd.AddPair('dateTime', '2024-06-15T11:00:00Z');
    jsonObjEnd.AddPair('timeZone', 'Asia/Shanghai');
    
    然后把这两个子对象添加到主事件JSON里:
    jsonObjEventResource.AddPair('start', jsonObjStart);
    jsonObjEventResource.AddPair('end', jsonObjEnd);
    
  • REST控件配置:确保你的REST客户端已经正确设置了Google OAuth2授权,包括:
    • 客户端ID和客户端密钥(从Google Cloud控制台获取)
    • 正确的API范围:https://www.googleapis.com/auth/calendar.events
    • 请求的URL是https://www.googleapis.com/calendar/v3/calendars/{calendarId}/events({calendarId}可以用primary表示当前用户的主日历)
  • 内存管理:一定要在finally块里释放所有创建的TJSONObject实例,避免内存泄漏,上面的代码我已经帮你补上了finally的框架。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 07:36:35