node.jsgoogle-cloud-platformgoogle-cloud-billing

Getting "The caller does not have permission" when trying to updateBillingInfo


I'm trying to programmatically update billing account for one of my projects. I'm using node client library for googleapis REST. This is my code:

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();
}

This is the error I get:

errors: [
    {
      message: 'The caller does not have permission',
      domain: 'global',
      reason: 'forbidden'
    }
  ]

I'm using a service account and environment variable is correctly set, because I can create projects and use other APIs.

My service account has these roles:

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

If enable billing from gcloud with same service account it works:

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

What am I missing?


Solution

  • the names of the parameters are not clear, try to use

    project id instead of the project name and instead of billing account name use billing account id

    const request = { 
          name: "projects/rensi-28",   // projectId
          requestBody: {
            billingAccountName: "billingAccounts/My Billing Account" //billingAcountId
          }    
               
      };