【问题标题】:How to get current event's conference data after use schedule conference in Google Calendar在谷歌日历中使用安排会议后如何获取当前事件的会议数据
【发布时间】:2020-04-03 03:52:51
【问题描述】:

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

问题:当用户点击添加会议安排会议(第三方服务,而不是环聊)时,如何获取当前活动的会议数据?我尝试使用Calendar.Events.get API,但它返回 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"
        }]
    }
}

【问题讨论】:

  • 你的evnetId是从哪里得到的?
  • 通过 eventUpdateTrigger,我可以从函数 onCalendarEventUpdate 的参数中获取evnetId
  • 您无法从Calendar.Events.get 获取evnetId,它恰恰相反——您需要向方法提供您以不同方式检索到的事件ID。例如,使用Calendar.Events.list,您可以列出所有事件,包括摘要和事件 ID 等所有重要信息。

标签: google-apps-script gsuite-addons


【解决方案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

【讨论】:

    【解决方案2】:

    根据错误消息,我猜您的 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

    【讨论】:

    • 感谢您的回答,我试过了,但似乎不行。我无法从 Calendar.Events.list(calendarId, {syncToken: nextSyncToken}) 的响应中获取事件,响应为 {"nextSyncToken":"xxxxx","accessRole":"owner","summary":"xxxx@address.com","kind":"calendar#events","timeZone":"Asia/Shanghai","defaultReminders":[{"method":"popup","minutes":10}],"etag":"\"pxxxxxxg\"","updated":"2020-04-08T06:01:32.362Z","items":[]}
    • @joyvince 看起来 items 数组是空的,这意味着没有更新任何事件。您可以创建脚本项目的副本并共享它吗?
    • 这几天发现了问题,其实我们需要在appscript.json中添加"currentEventAccess": "READ_WRITE"。添加后,我们可以从上下文中获取会议数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多