用php来操作google group,首先要完成权限验证,方法参考文章Google Group添加成员代码实例。本文介绍group的创建、删除等操作。
目录
创建一个新的Google Group
group email必须以注册到g suite账户里的域名结尾,这是一个私有群组。
$groupEmail = "grouname@yourdomain.com"; $groupDesc = 'A testing group'; $groupInstance = new Google_Service_Directory_Group(); $groupInstance->setEmail( $groupEmail ); $groupInstance->setDescription( $groupDesc ); try { $insertGroupResult = $service->groups->insert( $groupInstance ); echo "Success"; } catch ( Google_Service_Exception $gioe) { echo "Error in connection: ".$gioe->getMessage(); }
显示某个域名下的Group
$domain = "yourdomain.com"; $groupList = $service->groups->listGroups( array( 'domain' => $domain ) );
获取group信息
$groupKey = "groupname@yourdomain.com"; $theGroup = $service->groups->get( $groupKey );
删除group
$groupKey = "groupname@yourdomain.com"; $deleteGroup = $service->groups->delete( $groupKey );
更新group信息
$groupKey = "groupname@yourdomain.com"; $groupInstance = new Google_Service_Directory_Group(); $groupInstance->setDescription( 'New group description' ); $updateGroupResults = $service->groups->update( $groupKey, $groupInstance );
以上用到两个类:
Google_Service_Directory_Group, 用于创建group的实例,代码见google\apiclient-services\src\Google\Service\Directory\Group.php。
Google_Service_Directory_Resource_Groups,用于group的操作,代码见位于Service\Directory\Resource\Groups.php,在程序里直接使用$service->groups来访问这个方法的实例。
为group添加别名
group的别名也是一个邮件地址,必须具有唯一性,可以用作group key。
$groupKey = "groupname@yourdomain.com"; $groupaliases = "a-aliases@yourdomain.com"; $aliasesInstance = new Google_Service_Directory_Alias(); $aliasesInstance->setAlias( $groupaliases ); $insertAliasesResults = $service->groups_aliases->insert( $groupKey, $aliasesInstance);
删除group的别名
$groupKey = "groupname@yourdomain.com"; $groupaliases = "a-aliases@yourdomain.com"; $deleteAliasesResults = $service->groups_aliases->delete( $groupKey, $groupaliases );
操作别名用到两个类:
Google_Service_Directory_Alias,创建别名实例,代码见Service\Directory\Alias.php
Google_Service_Directory_Resource_GroupsAliases,操作别名的类,代码见Service\Directory\Resource\GroupAliases.php,在程序里通过$service->groups_aliases来访问。