这似乎可以自行修复......大约一个小时后,我按下 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__);
但是,当我运行此代码时,我得到
致命错误:在 /var/www/common/google- api- 中带有消息“调用 GET https://www.googleapis.com/admin/directory/v1/users : (400) 错误请求”的未捕获异常“Google_Service_Exception” php-client-master/src/Google/Http/REST.php:80
再次感谢您的洞察力。