这似乎解决了这个问题......大约一个小时后我点击了F5并且它有效,我可以看到书名列表。
所以我复制了文件并对其进行了修改,以获得所有用户的列表
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();
include_once "templates/base.php";
set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Directory.php';
$client_id = '118xxx.apps.googleusercontent.com';
$service_account_name = '118xxx@developer.gserviceaccount.com';
$key_file_location = '/Path/ServiceAccount-privatekey.p12';
echo pageHeader("Service Account Access");
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$service = new Google_Service_Directory($client);
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.user.readonly'),
$key
);
$cred->sub = "adminUser@googleAppsDomain.com";
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
$results = $service->users->listUsers();
echo "<h3>Results Of Call:</h3>";
print_r($results);
echo pageFooter(__FILE__);
但是,当我运行此代码时,我得到了
致命错误:未捕获的异常'Google_Service_Exception' 消息'调用GET时出错 https://www.googleapis.com/admin/directory/v1/users :( 400)糟糕 请求'进入 /var/www/common/google-api-php-client-master/src/Google/Http/REST.php:80
再次感谢您的见解。
答案 0 :(得分:1)
我可以帮助您超越 400 Bad Request 错误:
致电$service->users->listUsers()
需要一些参数 - 至少domain
或customer
。
E.g。 $service->users->listUsers( Array('domain' => 'test.com') )
这是您的直接问题 - 您对该API的调用不完整。
然而,在那之后,我愿意打赌你会遇到权限问题。我正在尝试在我的项目中解开它们!
答案 1 :(得分:-1)
工作代码在这里:Google php client library loadServiceAccountJson broken - fix enclosed