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

使用googleapis创建指定日历事件却同步至主日历的问题排查

解决方案:仅在指定日历创建事件,避免Primary日历重复

问题根源在于你将当前用户(即日历所有者)添加为了事件的参会者,并且设置了responseStatus: "accepted"。当Google日历检测到所有者接受了自己日历中的事件邀请时,会自动在其Primary日历中创建一个副本。

要实现仅在专用日历创建事件,只需修改以下两点:

  • 移除当前用户作为参会者的条目:事件本身已经存储在用户的专用日历中,无需再将自己添加为参会者。
  • (可选)调整sendUpdates参数:如果不需要给所有者发冗余通知,可以设置为"externalOnly",仅给外部参会者发送邀请。

修改后的代码如下:

export async function createCalendarEvent({
  clerkUserId,
  durationInMinutes,
  eventName,
  guestEmail,
  guestName,
  guestNotes,
  startTime,
}: {
  clerkUserId: string;
  durationInMinutes: number;
  eventName: string;
  guestEmail: string;
  guestName: string;
  guestNotes?: string | null;
  startTime: Date;
}) {
  const oAuthClient = await getOAuthClient(clerkUserId);
  const calendarUser = await clerkClient().users.getUser(clerkUserId);

  if (calendarUser.primaryEmailAddress == null) {
    throw new Error("Clerk user has no email");
  }

  const calendarEvent = await google.calendar("v3").events.insert({
    calendarId: process.env.GOOGLE_CALENDAR_ID,
    auth: oAuthClient,
    sendUpdates: "externalOnly",
    requestBody: {
      attendees: [
        { email: guestEmail, displayName: guestName }
        // 移除当前用户的参会者条目
      ],
      description: guestNotes ? `Additional Details: ${guestNotes}` : undefined,
      start: {
        dateTime: startTime.toISOString(),
      },
      end: {
        dateTime: addMinutes(startTime, durationInMinutes).toISOString(),
      },
      summary: `${guestName} + ${calendarUser.fullName}: ${eventName}`,
    },
  });

  return calendarEvent.data;
}

补充说明

  • 如果需要在事件中体现所有者信息,可将其写入summary或description字段,无需通过参会者身份添加。
  • sendUpdates参数可选值:"all"(给所有人发送通知)、"externalOnly"(仅外部参会者)、"none"(不发送任何通知),可根据业务需求选择。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 07:38:11