【发布时间】:2021-05-29 03:13:58
【问题描述】:
我正在尝试以编程方式更新我的一个项目的结算帐户。我正在为 googleapis REST 使用节点客户端库。这是我的代码:
const { google } = require('googleapis');
const cloudbilling = google.cloudbilling('v1');
async function main() {
let authClient;
try {
authClient = await authorize();
console.log('AUTH', authClient);
} catch (err) {
console.error(err);
}
const request = {
name: "projects/rensi-28",
requestBody: {
billingAccountName: "billingAccounts/My Billing Account"
}
};
google.options({auth: authClient});
try {
const response = (await cloudbilling.projects.updateBillingInfo(request)).data;
console.log(JSON.stringify(response, null, 2));
} catch (err) {
console.error('error:', err);
}
}
main();
async function authorize() {
const auth = new google.auth.GoogleAuth({
scopes: ['https://www.googleapis.com/auth/cloud-platform']
});
return await auth.getClient();
}
这是我得到的错误:
errors: [
{
message: 'The caller does not have permission',
domain: 'global',
reason: 'forbidden'
}
]
我正在使用服务帐户并且环境变量设置正确,因为我可以创建项目并使用其他 API。
我的服务帐户具有以下角色:
| Level | Role |
|---|---|
| Project | Project Billing Manager |
| Project | Owner |
| Organization | Billing Account Administrator |
| Organization | Billing Account Creator |
| Organization | Project Billing Manager |
| Organization | Billing Account Viewer |
| Organization | Owner |
| Billing Account | Billing Account Administrator |
| Billing Account | Billing Account User |
| Billing Account | Billing Account Viewer |
如果使用相同的服务帐户从 gcloud 启用计费,它可以工作:
gcloud alpha billing projects link p001 --billing-account 45FG32-45FG32-45FG32
gcloud alpha billing projects link p001 --billing-account 45FG32-45FG32-45FG32
billingAccountName: billingAccounts/45FG32-45FG32-45FG32
billingEnabled: true
name: projects/p001/billingInfo
projectId: bxtrb-rensi-28
我错过了什么?
【问题讨论】:
标签: node.js google-cloud-platform google-cloud-billing