I would like to know how to get the email address associated with the linked calendar

Viewed 89
2 Answers

Calendars dont have email addresses. They have calendar id's this for example would be a calendar id

v205qep4g260mm7fgq8vtgp18@group.calendar.google.com

It may look like an email address but it is not. You cant send it an email there for it is not an email address.

If you do a calendar.get on the primary calendar

curl \  'https://www.googleapis.com/calendar/v3/calendars/pramary?key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --compressed

The calendar id of that calendar is actually the email address of the currently authenticated user

{
 "kind": "calendar#calendar",
 "etag": "\"j32ipQvsbvz0_YlxYi3Ml2Fd7A\"",
 "id": "xxxxx@gmail.com",
 "summary": "Linda Lawton ",
 "description": "test",
 "timeZone": "Europe/Copenhagen",
 "conferenceProperties": {
  "allowedConferenceSolutionTypes": [
   "hangoutsMeet"
  ]
 }
}

This will however not work with other calendars the one above is my house hold cleaning calendar.

If what you are actually trying to do id get the email address of the owner of the calendar then you should be using acl.list this method will return to you all the rules for the calendar

curl \
  'https://www.googleapis.com/calendar/v3/calendars/p4g260mm7fgq8vtgp18%40group.calendar.google.com/acl?key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --compressed

One of those rules is the owners email

 {
   "kind": "calendar#aclRule",
   "etag": "\"00001501683456631000\"",
   "id": "user:XXXXX@gmail.com",
   "scope": {
    "type": "user",
    "value": "XXXXX@gmail.com"
   },
   "role": "owner"
  },

I altered the node quickstart a little to show how to use this method

/**
 * Lists the next 10 events on the user's primary calendar.
 * @param {google.auth.OAuth2} auth An authorized OAuth2 client.
 */
function listEvents(auth) {
  const calendar = google.calendar({version: 'v3', auth});
  calendar.acl.list({
    calendarId: 'primary',   
  }, (err, res) => {
    if (err) return console.log('The API returned an error: ' + err);
    const rules = res.data.items;
    if (rules .length) {
      
      rules .map((rule, i) => {        
        console.log(`User ${rule.id} with the role ${rule.role}`)
      });
    } else {
      console.log('No upcoming rules found.');
    }
  });
}

As @DalmTo said Google Calendar is associated with an id not corresponding to an email address, except in the case of your primary calendar.

As an implementation in NodeJS of what @DalmTo has provided on ACL:

async function getCalendarACL(auth) {
  // call the service                  
  const calendar = google.calendar({ version: 'v3', auth });
  console.log(Object.keys(calendar))
  // get all the calendars in the current user
  let res = await calendar.calendarList.list({})
  let calendars = await res.data.items
  for (let calendar_i of calendars) {
    let calendar_acl = await calendar.acl.list({ calendarId: calendar_i.id })
    let acl_rules = await calendar_acl.data.items
    console.log(`Calendar ${calendar_i.id}`)
    for (rule of acl_rules) {
      console.log(`User ${rule.id} with the role ${rule.role}`)
    }
  }
} 

If you get a group in the scope.typefield you need to use the Directory API, in order to get the emails of the users having access.

const directory = google.admin({version: 'directory_v1', auth});
const usersInGroup = await directory.members.list({groupKey:groupID})

Documentation

Related