I have enabled the Cloud Billing API service, then created a service account and set the permissions
Billing Account Usage Commitment Recommender Admin
, then I send my code, which returns 403
The following is the error returned:
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://cloudbilling.googleapis.com/v1/billingAccounts/XXXXX-XXXXX-XXXXX?alt=json returned “The caller does not have permission”. Details: “The caller does not have permission”>
from google.oauth2 import service_account
from googleapiclient.discovery import build
# Set service account file path
SERVICE_ACCOUNT_FILE = './1.json'
# Create credentials
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE)
# Build service object
service = build('cloudbilling', 'v1', credentials=credentials)
# Your billing account ID can be found in the Google Cloud Billing console
billing_account_id = 'billingAccounts/01C599-D72070-38D2DE'
# Get billing information
billing_info = service.billingAccounts().get(name=billing_account_id).execute()
print('Billing Account Info:')
print(billing_info)
# Get a list of bill items
projects = service.billingAccounts().projects().list(name=billing_account_id).execute()
print('Projects:')
for project in projects.get('projectBillingInfo', []):
print(f"{project['projectId']}: {project['billingAccountName']}")
I tried many times and there was no problem with the permissions! How can I call it
mi zhang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.