<?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>
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.
I m using Email as my groupKey.