Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a working example in PHP with Add and Delete.</p> <pre><code>function gmail_provision($email, $email_list, $requested_operation) { // requested_operation should be either add or delete require_once "google-api-php-client/src/Google_Client.php"; require_once "google-api-php-client/src/contrib/Google_DirectoryService.php"; require_once "google-api-php-client/src/contrib/Google_Oauth2Service.php"; session_start(); $group_scope = 'https://www.googleapis.com/auth/admin.directory.group'; $service_account_email = '.....@developer.gserviceaccount.com'; $service_account_pkcs12_file_path = '/path/to/...-privatekey.p12'; $client_id = '......apps.googleusercontent.com'; $adminEmail = 'admin-account@email.com; $key = file_get_contents($service_account_pkcs12_file_path); $auth = new Google_AssertionCredentials($service_account_email, array($group_scope), $key, 'notasecret', 'http://oauth.net/grant_type/jwt/1.0/bearer', $adminEmail); $client = new Google_Client(); $client-&gt;setClientId($client_id); // from API console $client-&gt;setApplicationName("API Project Name"); $client-&gt;setUseObjects(true); $client-&gt;setAssertionCredentials($auth); try { // Adding or removing from specified list $sync_status = ''; $service = new Google_DirectoryService($client); switch ($requested_operation) { case "add": // Add $member = new Google_Member(array('email' =&gt; $email, 'kind' =&gt; 'admin#directory#member', 'role' =&gt; 'MEMBER', 'type' =&gt; 'USER')); $service-&gt;members-&gt;insert($email_list, $member); break; case "remove": // Remove // Users must be removed by ID not by email address // This is slow and should be changed when // Google fixes the bug and allows delete by email. // Get a list of email addresses in the list $results = $service-&gt;members-&gt;listMembers($email_list); // Push results into an array $arrResults = get_object_vars($results); // Recursive iterator $arrIt = new RecursiveIteratorIterator(new RecursiveArrayIterator($arrResults)); // Cycle through the array searching for the email address foreach ($arrIt as $sub) { $subArray = $arrIt-&gt;getSubIterator(); if ($subArray['email'] === $email) { // Store an array of the details associated // with the email address $outputArray[] = iterator_to_array($subArray); } } // Delete by id $service-&gt;members-&gt;delete($email_list, $outputArray[0]['id']); break; } } catch (Google_ServiceException $e) { // $e-&gt;getCode() // $e-&gt;getMessage() } catch (Google_Exception $e) { // $e-&gt;getCode() // $e-&gt;getMessage() } catch (Zend_Gdata_Gapps_ServiceException $e) { // $e-&gt;getCode() // $e-&gt;getMessage() } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload