0

I wrote the following function in Apps Script to create a Calendar Event (based on information in a Google Sheet), but would like one of the attendees to be the owner so they can see the waiting room for guests. Can I transfer ownership of the event or is there another way to make that happen?

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;
}
Johan
  • 26
  • 6

2 Answers2

2

There are two options to assign the event ownership to a different user

1. Transfer the ownership of an already existing event

  • This is unfortunately not possible by setting the creator or organizer property, since those parameters are only read-only.
  • To transfer the ownership, you need to use the method Events: move. Note that in this case the event will be moved out of the calendar it is currently located into the calendar of the person who is meant be asisgned as the new owner

2. Use a service account with domain-wide delegation

  • This allows you perform requests on behalf of any user of your domain
  • In other words, you can create events on another user's behalf by impersonating him.
  • In Apps Script you do so by using the OAuth2 library
  • See here for a sample
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • I am interested in using the second bullet of the first point. I have looked into using the Events: move method, but have not been able to get that working. Can you tell me what I'm doing wrong in the code below? `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"); }` – Johan Nov 27 '20 at 12:28
  • update: Ah, you need to have writing access for the other calendar as well. So maybe a service account with domain-wide delegation will do the trick there as well. – Johan Nov 27 '20 at 13:21
  • 1
    Yes, it will also do the trick. However, when using a service account you need to to build your request differently than in your comment. You need to build a `service` and then perform a UrlFetch request to https://www.googleapis.com/calendar/v3/calendars/calendarId/events/eventId with `service.getAccessToken()` for authentication INSTEAD of Calendar.Events.get. It is a very different process. See the sample I linked in my answer. – ziganotschka Nov 27 '20 at 14:22
  • Thank you for your help, @ziganotschka I appreciate it very much. What am I doing wrong here? https://pastebin.com/eD5rja6n – Johan Nov 28 '20 at 15:25
  • having issues logging back into pastebin, so here's an updated code (though it gives me an error 404 currently, at least there's some progress): https://wtools.io/paste-code/b2Jr – Johan Nov 29 '20 at 10:30
  • Does `user` have editor access to the calendar? – ziganotschka Nov 29 '20 at 15:48
  • The service account has editor access to all calendars in the domain. The user is a service account user linked to the service account. – Johan Dec 01 '20 at 08:21
  • When you use impersonation it is not relevant either the service account has editor access to the calendar. It is the impersonated user who needs to have editor access to the calendar. – ziganotschka Dec 01 '20 at 08:27
1

Set the guestsCanSeeOtherGuests property to true.

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

If using CalendarApp, you can call .setGuestsCanSeeGuests(true) on the CalendarEvent.

Diego
  • 9,261
  • 2
  • 19
  • 33