【发布时间】:2021-12-01 03:28:57
【问题描述】:
我们正在尝试检索 GCP 中的结算帐户列表,为此,我们正在使用服务帐户。我们写的代码如下:
async function listBillingAccounts(project)
{
let keys = JSON.parse(Buffer.from(process.env.SERVICE_ACCOUNT_BASE64_JSON, "base64").toString("ascii"));
const client = new CloudBillingClient({credentials : keys});
if (project !== undefined)
{
const [accounts] = await client.listBillingAccounts({ project, });
console.log(accounts);
}
else
{
const [accounts] = await client.listBillingAccounts({});
console.log(accounts);
}
console.info(`found ${accounts.length} billing accounts:`);
for (const account of accounts) {
console.info(`${account.displayName}:`);
console.info(`\topen: ${account.open}`);
console.info(`\tparentBillingAccount: ${account.masterBillingAccount}`);
}
}
SERVICE_ACCOUNT_BASE64_JSON 包含服务帐户信息:
{
"type": "service_account",
"project_id": "xxxx",
"private_key_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"private_key": "-----BEGIN PRIVATE KEY----------END PRIVATE KEY-----\n",
"client_email": "xxxxx@xxxxx",
"client_id": "xxxxxxxxxxxxxxxxxxxxx",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/xxxxx"
}
无论我们是否指定项目名称,accounts 结果总是一个空数组。你知道会发生什么吗?我们会询问 IAM 团队,服务帐号是否具有所需的权限,并且他们确认它具有:
https://www.googleapis.com/auth/cloud-billing.readonly --> Billing Account Usage Commitment Recommender Viewer 对吗?
【问题讨论】:
标签: node.js google-cloud-platform google-api