在PHP中显示Google Admin SDK JSON响应
多亏了Emily,我已经找到了php客户端库。我需要检索一个用户数据。API中有一个函数可以执行此操作: Google_DirectoryService.php在PHP中显示Google Admin SDK JSON响应,php,json,google-admin-sdk,Php,Json,Google Admin Sdk,多亏了Emily,我已经找到了php客户端库。我需要检索一个用户数据。API中有一个函数可以执行此操作: Google_DirectoryService.php /** * retrieve user (users.get) * * @param string $userKey Email or immutable Id of the user * @param array $optParams Optional parameters. * @return Google_User *
/**
* retrieve user (users.get)
*
* @param string $userKey Email or immutable Id of the user
* @param array $optParams Optional parameters.
* @return Google_User
*/
public function get($userKey, $optParams = array()) {
$params = array('userKey' => $userKey);
$params = array_merge($params, $optParams);
$data = $this->__call('get', array($params));
if ($this->useObjects()) {
return new Google_User($data);
} else {
return $data;
}
}
index.php
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DirectoryService.php';
$client = new Google_Client();
$client->setApplicationName("IAA SIS");
$client->setClientId('xxx.apps.googleusercontent.com');
$client->setClientSecret('xxx');
$client->setRedirectUri('http://mypage.com/oauth2callback');
$client->setDeveloperKey('xxx');
$client->setScopes(array('https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/admin.directory.user.readonly'));
$service = new Google_DirectoryService($client);
$email = filter_var($user['email'], FILTER_SANITIZE_EMAIL); // get the USER EMAIL ADDRESS using OAuth2
$results = $service->users->get('$email', 'public', $optParams); //Pass email to function parameter ? I'm not sure.
?>
此外,我在admin.google.com上选中了“启用API访问”,并在google API控制台上启用了admin SDK
有人能给我一些帮助吗?你有没有检查过谷歌API PHP客户端库?() 此外,他们还有一个github页面:
一旦你从网站下载了这个库,你可以看到这个库有Google_DirectoryService最后,我想出了如何使用Google目录服务API调用这个数组。请参考以下代码:
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_PlusService.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';
require_once 'google-api-php-client/src/contrib/Google_DirectoryService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("ApplicationName");
//*********** Replace with Your API Credentials **************
$client->setClientId('Your_Client_ID');
$client->setClientSecret('Your_Client_Sercret');
$client->setRedirectUri('http://example.com/oauth2callback');
$client->setDeveloperKey('Your_API_Key');
//************************************************************
$client->setScopes(array('https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/admin.directory.user.readonly'));
$plus = new Google_PlusService($client);
$oauth2 = new Google_Oauth2Service($client); // Call the OAuth2 class for get email address
$adminService = new Google_DirectoryService($client); // Call directory API
if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
}
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['access_token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['access_token'])) {
$client->setAccessToken($_SESSION['access_token']);
}
if ($client->getAccessToken()) {
$user = $oauth2->userinfo->get();
$me = $plus->people->get('me');
$email = filter_var($user['email'], FILTER_SANITIZE_EMAIL); // get the USER EMAIL ADDRESS using OAuth2
$optParams = array('maxResults' => 100);
$activities = $plus->activities->listActivities('me', 'public', $optParams);
$users = $adminService->users->get($email);
$_SESSION['access_token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
}
?>
你好,艾米丽。API PHP客户端库主要用于Google API控制台上提供的服务,但不用于Admin SDK。他们看起来不一样。我找不到任何管理SDK的php客户端库。这是否意味着php目前不支持它?Admin SDK实际上只是专门为管理员设计的所有API套件的名称(目录API、报告API、站点验证API……都是Admin SDK的一部分)。从最初的问题来看,您对使用目录API(它是AdminSDK的一部分)感兴趣。因此,如果您查看PHP客户端库,您将看到包括目录服务在内的单个服务。我在目录API中找不到PHP客户端库:再次感谢您的帮助。它不在页面中,但如果您在我上面提供的链接中下载PHP客户端。您将在软件包中看到它。:)(我知道这很混乱…)谢谢艾米丽。我终于找到了php文件,命名太混乱了。让我先看看。再次感谢。我用来做测试的网站是php.s3uc.com
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_PlusService.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';
require_once 'google-api-php-client/src/contrib/Google_DirectoryService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("ApplicationName");
//*********** Replace with Your API Credentials **************
$client->setClientId('Your_Client_ID');
$client->setClientSecret('Your_Client_Sercret');
$client->setRedirectUri('http://example.com/oauth2callback');
$client->setDeveloperKey('Your_API_Key');
//************************************************************
$client->setScopes(array('https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/admin.directory.user.readonly'));
$plus = new Google_PlusService($client);
$oauth2 = new Google_Oauth2Service($client); // Call the OAuth2 class for get email address
$adminService = new Google_DirectoryService($client); // Call directory API
if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
}
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['access_token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['access_token'])) {
$client->setAccessToken($_SESSION['access_token']);
}
if ($client->getAccessToken()) {
$user = $oauth2->userinfo->get();
$me = $plus->people->get('me');
$email = filter_var($user['email'], FILTER_SANITIZE_EMAIL); // get the USER EMAIL ADDRESS using OAuth2
$optParams = array('maxResults' => 100);
$activities = $plus->activities->listActivities('me', 'public', $optParams);
$users = $adminService->users->get($email);
$_SESSION['access_token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
}
?>