我正在尝试使用以下代码以编程方式创建一个结算帐户:
use Google\Auth\ApplicationDefaultCredentials;
use Google\Cloud\Billing\V1\CloudBillingClient;
use Google\Cloud\Billing\V1\BillingAccount;
putenv('GOOGLE_APPLICATION_CREDENTIALS='.dirname(__FILE__).'/manager-307215-df4011b08eab.json');
$scopes = ['https://www.googleapis.com/auth/cloud-platform','https://www.googleapis.com/auth/service.management'];
$gclient = new Google\Client();
$gclient->useApplicationDefaultCredentials();
$gclient->addScope($scopes);
$gclient->setApiFormatV2(true);
$billingClient = new CloudBillingClient();
$billingAccount = new BillingAccount([
'display_name' => 'My Billing Account',
'master_billing_account' => ''
]);
try {
$billingClient->createBillingAccount($billingAccount);
} catch (Exception $e) {
echo 'FAILED TO CREATE BILLING ACCOUNT ('.$e->getMessage().')<br>';
}
$billingAccount = $billingClient->getBillingAccount('billingAccounts/015B13-FE2CD5-0CC6A2');
$billingAccount->setDisplayName('TestFROMPHP');
try {
$billingClient->updateBillingAccount('billingAccounts/015B13-FE2CD5-0CC6A2', $billingAccount);
} catch (Exception $e){
echo 'FAILED TO EDIT BILLING ACCOUNT ('.$e->getMessage().')<br>';
}
$accounts = $billingClient->listBillingAccounts();
foreach ($accounts as $account) {
echo 'Billing account: ' . $account->getName() . '<br>';
}
服务帐户具有管理员计费角色,应该能够创建/修改计费帐户,但每种方法都会出现例外情况。
我错过了什么吗?