使用Google API服务查找组中的成员列表 [英] Find list of Members in group using google api services
问题描述
<?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客户端php查找群组的所有成员.但是我不知道为什么当我使用auth连接到google并允许访问目录的权限时,重定向到它的不返回列表.
I have implemented this example in my local system to find all members of groups using google api client php. but i dont know why when i m connect to google using auth and allow permission to access directory, when redirect its not return list.
所以请帮助我,并建议我这段代码在哪里做错了.
So please help me and suggest me where i m doing wrong in this code.
我使用Email
作为我的groupKey
.
推荐答案
以下是我写的一些代码,我认为可能会对您有所帮助.此代码将列出您具有读取权限的所有组及其成员资格的列表.
Here is some code I wrote that I think might help you. This code will pull a listing of all the groups you have read access to and their memberships.
拉出组时,我检查是否设置了变量nextPageToken.如果设置了该值,则意味着您已达到200个最大限制结果集.如果设置了nextPageToken,则代码将继续循环并获取下一组,直到nextPageToken为空.
When pulling the groups, I check if the variable nextPageToken is set. If this is set, it means you have reached the 200 max limit result set. If nextPageToken is set, the code will continue to loop and grab the next set until nextPageToken is blank.
在第一遍中,我使用批处理类来获取组成员身份以加快处理速度.第一次通过后,我再次检查是否存在nextPageToken变量,并继续循环直到返回所有成员.
I make use of the batch class in the first pass at getting group memberships to speed things up. After the first pass, I again then check for the existence of the nextPageToken variable and continue to loop until all members are returned.
脚本运行后,将使用所有组及其各自的成员资格填充名为$ lists的数组.
After the script runs, an array called $lists is populated with all the groups and their respective memberships.
<?php
$start = microtime(true);
require_once('/path/to/google-api-php-client/src/Google/autoload.php');
$service_account_name = 'your service account address'; //Email Address
$key_file_location = '/path/to/your.p12'; //key.p12
$client = new Google_Client();
$client->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";
这篇关于使用Google API服务查找组中的成员列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!