1

背景:谷歌日历 > 点击新建按钮 > 进入新活动页面 > 添加会议

点击这里安排会议

问题:当用户点击添加会议安排会议(第三方服务,不是环聊)时,如何获取当前活动的会议数据?我尝试使用Calendar.Events.getAPI,但它返回 404。

我的 appscripts 设置在这里:

当用户安排会议时,它会触发onCalendarEventUpdate功能

{
  "timeZone": "America/Los_Angeles",
  "addOns": {
    "calendar": {
      "eventUpdateTrigger": {
        "runFunction": "onCalendarEventUpdate"
      },
    }
  }
}

我的onCalendarEventUpdate

function onCalendarEventUpdate(context: any) {
  // I can get calendarId, evnetId
  const {
    calendar: { calendarId, id: evnetId }
  } = context;

  // when I try to get event conferenceData, it returns 404
  let event;
  try {
    event = Calendar.Events && Calendar.Events.get(calendarId, evnetId);
    if (!event) {
      Logger.log(`[getEventsCollectionByCalendarId]event not found`);
    }
  } catch (e) {
    Logger.log(`[getEventsCollectionByCalendarId]error: ${JSON.stringify(e)}`);
  }
}

现在错误消息是:

{
    "message":"API call to calendar.events.get failed with error: Not Found",
    "name":"GoogleJsonResponseException",
    "lineNumber":64,
    "details":{
        "message":"Not Found",
        "code":404,
        "errors":[{
            "domain":"global",
            "reason":"notFound",
            "message":"Not Found"
        }]
    }
}
4

2 回答 2

1

我现在找到了解决方案,希望对其他人有用。

首先更新清单文件:

{
  "timeZone": "America/Los_Angeles",
  "oauthScopes": [
     "https://www.googleapis.com/auth/calendar.addons.current.event.read",
     "https://www.googleapis.com/auth/calendar.addons.current.event.write"
  ],
  "addOns": {
    "calendar": {
      "currentEventAccess": "READ_WRITE",
      "eventUpdateTrigger": {
        "runFunction": "onCalendarEventUpdate"
      },
    }
  }
}

然后在onCalendarEventUpdate函数中

function onCalendarEventUpdate(context) {
  const { conferenceData } = context;

  console.log('[onCalendarEventUpdate]conferenceData:', conferenceData);
}

您可以在这里成功获取会议数据

参考文档: https ://developers.google.com/apps-script/manifest/calendar-addons

于 2020-04-21T10:51:50.143 回答
0

根据错误消息,我猜您的 calendarId 和 eventId 无效。事件更新事件遗憾地没有给你事件ID。因此,您需要执行增量同步以获取更新的事件数据,这意味着您需要首先按照文档(下面的链接)中的说明进行初始同步。

首先,运行此代码以执行初始同步并获取每个日历的 nextSyncTokens。您只需要运行一次。

function initialSyncToSetNextSyncTokens() {
  const calendarIds = Calendar.CalendarList.list()["items"].map((item) => {
    return item["id"]
  });
  for (let calendarId of calendarIds) {
    let options = {maxResults: 2500, nextPageToken: undefined}
    let response = {}
    do {
      response = Calendar.Events.list(calendarId, options)
      options["nextPageToken"] = response["nextPageToken"]
    } while (options["nextPageToken"])
    PropertiesService.getScriptProperties().setProperty(calendarId, response["nextSyncToken"])
  }
}

然后,设置您的触发器以运行此功能并记录会议数据。请注意,我们还更新了 nextSyncToken 以便下一次执行能够正常工作。

function onEventUpdated(context) {
  const calendarId = context["calendarId"]
  const nextSyncToken = PropertiesService.getScriptProperties().getProperty(calendarId)
  const response = Calendar.Events.list(calendarId, {syncToken: nextSyncToken})
  PropertiesService.getScriptProperties().setProperty(calendarId, response["nextSyncToken"])
  const event = response["items"][0] // assumes this code will run before another event is created
  const conferenceData = event["conferenceData"]
  console.log(conferenceData)
}

链接到相关文档:

https://developers.google.com/apps-script/guides/triggers/events#google_calendar_events

于 2020-04-03T13:57:25.067 回答