I recently was tasked with pulling a listing of all my organizations Google Groups along with their membership for an internal application. My programming language of choice is PHP. Thankfully, Google has provided a nice PHP library for accessing their api’s.
There appears to be very few examples currently available for doing what I needed, so after I created my script I decided it might be of use to someone else.
setApplicationName("Client_Library_Examples");
$dir = new Google_Service_Directory($client);
$domain = 'yourdomain.com';
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array('https://www.googleapis.com/auth/admin.directory.group.readonly'),
$key
);
$cred->sub = "admin@yourdomain.com"; // a privilidged users email address
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
// Get all of our groups
$googleGroups = $dir->groups->listGroups(array('domain'=>$domain));
$groups = $googleGroups->getGroups();
// if we have a paginated result set, continue doing a lookup until we are at the end
while (isset($googleGroups->nextPageToken) && ($googleGroups->nextPageToken != '')) {
$googleGroups = $dir->groups->listGroups(array('domain'=>$domain,'pageToken'=>$googleGroups->nextPageToken));
$groups = array_merge($groups, $googleGroups->getGroups());
}
// Ok, we have all our groups. Now lets build an array that we can use later to populate with group members
$lists = array();
foreach ($groups as $key=>$val) {
$lists[$val->id] = array('id'=>$val->id,'description'=>$val->description,'email'=>$val->email,'name'=>$val->name,'members'=>'');
}
// Doing a batch query for all members of the groups to reduce execution time
$client->setUseBatch(true);
$batch = new Google_Http_Batch($client);
foreach ($groups as $k=>$v) {
$members = $dir->members->listMembers($v->email);
$batch->add($members, $v->id);
}
// execute our batch query
$results = $batch->execute();
// turn off batch queries now so we can query for more members in the case of paginated result sets
$client->setUseBatch(False);
foreach ($results as $k=>$v) {
$all = $v->getMembers();
$id = substr($k, 9); // the array key is in the form of response-XXX, lets get rid of the response- bit
while (isset($v->nextPageToken) && ($v->nextPageToken != '')) {
$members = $dir->members->listMembers($lists[$id]['email'],array("pageToken"=>$v->nextPageToken));
$v->nextPageToken = $members->nextPageToken;
$all = array_merge($all, $members->getMembers());
}
foreach ($all as $key=>$val) {
$lists[$id]['members'][] = $val->email;
}
}
$time_elapsed_secs = microtime(true) - $start;
print "\n\n" . $time_elapsed_secs . "\n\n";
The script will build an array called $lists that contains the names and memberships of all your org’s groups. I hope this helps someone solve the problem I had.