【问题标题】:can I set/change the owner Google Calendar event using an Apps script?我可以使用 Apps 脚本设置/更改所有者 Google 日历事件吗?
【发布时间】:2020-11-24 14:43:34
【问题描述】:

我在 Apps 脚本中编写了以下函数来创建日历活动(基于 Google 表格中的信息),但希望其中一位与会者成为所有者,以便他们可以看到客人的等候室。我可以转让活动的所有权还是有其他方法可以实现这一点?

function createEvent(title,start,end,teacher,student) {
  // https://developers.google.com/apps-script/advanced/calendar
  // https://developers.google.com/calendar/v3/reference/events
  var event = {
    summary: title,
    description: 'Teacher / student meeting',
    start: {
      dateTime: start.toISOString()
    },
    end: {
      dateTime: end.toISOString()
    },
    guestsCanInviteOthers: "no",
    conferenceData: {
      createRequest: {
        conferenceSolutionKey: {
          type: "hangoutsMeet"
        },
        requestId: start,
      },
    },
    attendees: [
    {email: teacher},
    {email: student}
    ],
  };
    event = Calendar.Events.insert(event, calendarId, {sendNotifications: false, conferenceDataVersion: 1} );
  return event.id;
}

【问题讨论】:

标签: google-apps-script google-calendar-api


【解决方案1】:

有两个选项可以将事件所有权分配给不同的用户

1.转让现有活动的所有权

  • 很遗憾,这无法通过设置creatororganizer 属性来实现,因为这些参数只是read-only
  • 要转让所有权,需要使用Events: move方法。请注意,在这种情况下,活动将从当前所在的日历中移出,并移至被指定为新所有者的人的日历中

2。使用service accountdomain-wide delegation

  • 这允许您代表您域的任何用户执行请求
  • 换句话说,您可以通过冒充其他用户来代表他创建事件。
  • 在 Apps 脚本中,您可以使用 OAuth2 library
  • 请参阅here 获取示例

【讨论】:

  • 我有兴趣使用第一点的第二个项目符号。我已经研究过使用 Events: move 方法,但无法使其正常工作。你能告诉我我在下面的代码中做错了什么吗? function move(event_id) { var calendar = CalendarApp.getDefaultCalendar();; var calendarId = 'primary'; if (!event_id) { event_id = "567031pjq65e18o9nj92vos050"; } var event = Calendar.Events.get(calendarId, event_id, {}); Calendar.Events.move(calendarId, event_id, "email@domain.com"); }
  • 更新:啊,您还需要拥有其他日历的写入权限。因此,也许具有域范围委派的服务帐户也可以解决问题。
  • 是的,它也可以解决问题。但是,在使用服务帐户时,您需要以不同于评论的方式构建您的请求。您需要构建一个service,然后使用service.getAccessToken()googleapis.com/calendar/v3/calendars/calendarId/events/eventId 执行UrlFetch 请求,以代替Calendar.Events.get 进行身份验证。这是一个非常不同的过程。请参阅我在答案中链接的示例。
  • 感谢您的帮助,@ziganotschka 我非常感谢。我在这里做错了什么? pastebin.com/eD5rja6n
  • 在重新登录到 pastebin 时遇到问题,所以这里有一个更新的代码(虽然它目前给我一个错误 404,但至少有一些进展):wtools.io/paste-code/b2Jr
【解决方案2】:

guestsCanSeeOtherGuests 属性设置为true

var event = {
  // ... 
  guestsCanInviteOthers: false, // expected a boolean, not the string "no"
  guestsCanSeeOtherGuests: true
};

如果使用CalendarApp,您可以拨打.setGuestsCanSeeGuests(true)CalendarEvent

【讨论】:

    猜你喜欢
    • 2022-08-09
    • 1970-01-01
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    相关资源
    最近更新 更多