下面我根据GCP文档here使用了PHP代码,以获取可以使用所选服务帐户访问的所有项目的列表。
require __DIR__ . '/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=acc-key.json');
$client = new Google_Client();
$client->setApplicationName('SampleApp');
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/cloud-platform');
$service = new Google_Service_Bigquery($client);
$optParams = [];
do {
$response = $service->projects->listProjects($optParams);
foreach ($response['projects'] as $project) {
print_r($project);
}
$optParams['pageToken'] = $response->getNextPageToken();
} while ($optParams['pageToken']);
这确实有效,并且可以使用此密钥访问的总共25个项目中仅提取一个或两个项目。
我通过提供其他项目名称的同时访问其他项目来确认对这些项目的许可。
任何提示可能是缺少的部分?
编辑:[基于米哈伊尔的回答]
如果我更改,我将得到以下错误
$service = new Google_Service_Bigquery($client);
到
$service = new Google_Service_CloudResourceManager($client);
我遇到错误
PHP Notice: Undefined property: Google_Service_CloudResourceManager::$projects in /tests/ListFullSchema.php on line 18
Notice: Undefined property: Google_Service_CloudResourceManager::$projects in /tests/ListFullSchema.php on line 18
PHP Fatal error: Uncaught Error: Call to a member function listProjects() on null in /tests/ListFullSchema.php:18
Stack trace:
#0 {main}
thrown in /tests/ListFullSchema.php on line 18
Fatal error: Uncaught Error: Call to a member function listProjects() on null in /tests/ListFullSchema.php:18