这似乎解决了自己..。大约一个小时后,我点击了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-php-client-master/src/Google/Http/REST.php:80中带有“错误调用GET https://www.googleapis.com/admin/directory/v1/users:(400)坏请求”消息的未命名异常“Google_Service_Exception”
再次感谢你的洞察力。
发布于 2014-03-06 20:02:16
我可以帮助您通过400坏请求错误:
调用$service->users->listUsers()需要一些参数--至少是domain或customer。
例如$service->users->listUsers( Array('domain' => 'test.com') )
这是你眼前的问题--你对那个API的调用是不完整的。
然而,在那之后,我愿意打赌你会遇到权限问题。我目前正试图解开他们在我的项目!
发布于 2015-07-27 19:50:32
丹·莱斯特和OP:我也遇到了这样的情况,终于能让它正常工作了。本质上,您需要设置域、view_type、和子。(其中sub是域中的用户,可根据.)提出请求。)php库中添加了用于设置服务授权的代码loadServiceAccountJson(),但是它在调用Google_Auth_AssertionCredentials时没有设置子程序,因此它毫无用处。
工作代码在这里:Google php client library loadServiceAccountJson broken - fix enclosed
https://stackoverflow.com/questions/22104184
复制相似问题