使用google api服务查找群组中的会员列表

时间:2015-08-08 09:40:16

标签: php google-api-php-client

<?php
include_once "templates/base.php";
session_start();

require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');

$client_id = '*******';
$client_secret = '*******';
$redirect_uri = '*********';

$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);

$client->addScope("https://www.googleapis.com/auth/admin.directory.group");

$directory = new Google_Service_Directory($client);

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  $client->setAccessToken($_SESSION['access_token']);
} else {
  $authUrl = $client->createAuthUrl();
}

if ($client->getAccessToken())
{
    $groupKey = "MY_EMAIL";
    $group = $directory->members->listMembers($groupKey);

    $_SESSION['access_token'] = $client->getAccessToken();
}

echo pageHeader("Group Members");

if (strpos($client_id, "googleusercontent") == false) {
  echo missingClientSecretsWarning();
  exit;
}
?>
<div class="box">
      <div class="request">
        <?php 
        if (isset($authUrl)) {
          echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
        } else {
          echo "<a class='logout' href='?logout'>Logout</a>";
        }
        ?>
      </div>

      <div class="shortened">
        <?php
        if (isset($group)) {
          var_dump($group);
        }
        ?>
      </div>
</div>

我已在本地系统中实现此示例,以使用google api client php查找所有组成员。但我不知道为什么当我使用auth连接谷歌并允许访问目录时,重定向其不返回列表。

所以请帮助我并建议我在这段代码中做错了。

我使用Email作为groupKey

2 个答案:

答案 0 :(得分:0)

https://developers.google.com/admin-sdk/directory/v1/reference/members/list

您可以查看此链接,这是它的“GET”

GET https://www.googleapis.com/admin/directory/v1/groups/groupKey/members

答案 1 :(得分:0)

public function getAllGroupMembers($groupid, $role = null)
{

     $allmembers = array();
      try {
             $pageToken = NULL;

    $optParams = array(
        'roles' => $role,
       
    );
        do {
            if ($pageToken) {
                $optParams['pageToken'] = $pageToken;
            }
            try {
                $results = self::$service_directory->members->listMembers($groupid,$optParams);
            } catch (Exception $e) {
                print 'An error occurred: ' . $e->getMessage() . 'calling listmemembers function';
            }
            $pageToken = $results->getNextPageToken();
               
            $allmembers = array_merge($allmembers, $results->getMembers()); 

            
        } while ($pageToken);
    } catch (Exception $e) {
        print 'An error occurred: ' . $e->getMessage();
    }
 
   return  $allmembers;
    
    
}