Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if Google Calendar API attendee is a group email?

Google Calendar API makes it easy to tell if an "attendee" is actually a resource:

https://developers.google.com/google-apps/calendar/v3/reference/events#resource

However, how can I tell if an "attendee" is actually a group, as opposed to a single person. ie. [email protected] vs [email protected]

like image 649
Hyon Lee Avatar asked Oct 19 '22 16:10

Hyon Lee


2 Answers

As of December 2019, this is still not possible with the Google Calendar API, but it is possible if you are a G Suite domain administrator and have access to the G Suite Admin SDK Directory API.

Here's an example function which uses the Admin SDK Directory Service in Google Apps Script.

You send an email address string to getGSuiteAddressType() and it returns an object with the address type from the following:

  • user
  • userAlias
  • group
  • groupAlias
  • null (if that address was not found in the G Suite domain)

And it also has the following extra features which may be convenient for G Suite developers:

  • If the address is a userAlias or groupAlias, it will also return the address which it is an alias of.
  • If the address is a user or userAlias, it will also return the user's last login time (or null if they have never logged in before)
  • If the address is a group or groupAlias, it will return an array of email addresses of the group members.

This uses Google Apps Script but the API can be accessed using any of the languages listed in the Quickstarts sidebar section of this page.

function getGSuiteAddressTypeTest() {

    getGSuiteAddressType('[email protected]'); // returns addressType "group"
    getGSuiteAddressType('[email protected]'); // returns addressType "groupAlias"

}

function getGSuiteAddressType(email) {

    email = email.toLowerCase();

    var data = {
        email: email,
        addressType: null,
        aliasOf: null,
        lastLoginTime: null
    };

    // check user and aliases

    try {

        var user = AdminDirectory.Users.get(email);

        data.addressType = 'user';

        for (var i = 0; i < user.aliases.length; i++) {

            var alias = user.aliases[i].toLowerCase();

            if (email === alias) {

                data.addressType = 'userAlias';
                data.aliasOf = user.primaryEmail;

            }

        }

    } catch (e) {}

    if (data.addressType) {

        var lastLoginTimestamp = user.lastLoginTime; // this outputs an ISO 8601 string that can be used to create a new date object

        if (lastLoginTimestamp === '1970-01-01T00:00:00.000Z') {

            Logger.log('the user has never logged in');

        } else {

            data.lastLoginTime = new Date(lastLoginTimestamp);

        }

        return data;

    }

    try {

        var group = AdminDirectory.Groups.get(email);

        data.addressType = 'group';

        if (group.aliases) {

            for (var i = 0; i < group.aliases.length; i++) {

                if (email === group.aliases[i].toLowerCase()) {

                    data.addressType = 'groupAlias';
                    data.aliasOf = group.email;

                }

            }

        }

        data.members = [];

        var groupMembers = getMembersOfGroup(email);

        for (var i = 0; i < groupMembers.length; i++) {

            data.members.push(groupMembers[i].email);

        }

    } catch (e) {}

    return data;

}

function getMembersOfGroup(groupEmail) {

    var pageToken, page;

    var members = [];

    do {

        try {

            page = AdminDirectory.Members.list(groupEmail, {
                pageToken: pageToken
            });

            if (page.members) {

                members = members.concat(page.members);

            }

            pageToken = page.nextPageToken;

        } catch (e) {}


    } while (pageToken);

    return members;

}
like image 140
Employee Avatar answered Oct 22 '22 23:10

Employee


This is currently not possible with Calendar API v3.

like image 29
luc Avatar answered Oct 22 '22 22:10

luc