3

There seems to be a similar question already, but I need a solution (read on) that authenticates as the current user.

I'm trying to make a simple Google Apps Script app that uses Google Calendar to check if your coworker (within a Google Apps for Work domain) is currently in a meeting.

The script would execute as the current user. To do this, I need to be able to get a list of all calendars available to the current user (not just ones they've subscribed), the same list that's available in the dropdown in the standard Calendar web app:

google calendar look up coworkers' calendars

The CalendarApp.getAllCalendars() gets all calendars that the user owns or is subscribed to, which is not what I need. I want the full list of calendars available to me within my Google Apps for Work domain (e.g. shared with the entire company), even if I'm not subscribed to them.

The CalendarList REST API available to Apps Scripts via Advanced Calendar Service has the same limitation.

The page Calendar for Work Features also mentions domain-wide authority delegation:

Accessing domain calendars as an app

An app can access domain-owned calendars without requiring user credentials if it authenticates using a service account. The service account must have the necessary access using domain-wide authority delegation.

which may get me what I need, but I don't want my script to run with admin rights, I want it to authenticate as the current user accessing it.

Is there a way?

Dmitry Pashkevich
  • 13,431
  • 9
  • 55
  • 73

1 Answers1

0

you need to subscribe to the calendar you want to see (and to unsubscribe it when you finished seeing it). Here a little demo:

var calendar = "name@domain.ext";

function getCalInfo(){
  var cal = CalendarApp.getCalendarById(calendar);
  Logger.log(cal);
  Logger.log(typeof cal);
  var unsubscribe = false;
  if(cal==null){
    try{
      Logger.log("subscribing");
      cal = CalendarApp.subscribeToCalendar(calendar);
      unsubscribe = true;
    }
    catch(err){
      Logger.log(err);
     return; 
    }
  }
  if(cal){
    Logger.log(cal.getTimeZone());
  }
  if(unsubscribe){
    Logger.log("unsubscribing");
    cal.unsubscribeFromCalendar();
  }
}
Harold
  • 3,297
  • 1
  • 18
  • 26
  • 1
    But how do I get a list of all the calendars I'm not subscribed to? Where does the `calendar` variable come from in your snippet? – Dmitry Pashkevich Jun 10 '16 at 15:45
  • @DmitryPashkevich that's not possible, if you want to get the list of all the calendar in the domain, you could try to get the list of all your user (as an admin function getUsers(){ var users = AdminDirectory.Users.list({domain:"domain.ext",fields:"users/primaryEmail"}); Logger.log(users); } The "calendar" var in the post code is the email address of a user. – Harold Jun 10 '16 at 16:32
  • See my question: "I don't want my script to run with admin rights, I want it to authenticate as the current user accessing it." – Dmitry Pashkevich Jun 10 '16 at 20:14