I created a GCP service account and assigned the needed permissions for listing the projects inside the organization. When I’m using the gcloud cli
, everything works:
JavaScript
x
5
1
gcloud auth activate-service-account --key-file=./key.json
2
gcloud projects list
3
4
# -> List of all projects
5
But when I try the “same” with the Python Client for Google Cloud Resource Manager, I receive a 403 The caller does not have permission
error message.
JavaScript
1
22
22
1
# pip install google-cloud-resource-manager==1.4.1
2
from google.oauth2 import service_account
3
from google.cloud import resourcemanager_v3
4
5
# Load gcp credentials
6
credentials = service_account.Credentials.from_service_account_file('./key.json')
7
8
# Create resourcemanager_v3 ProjectsClient
9
resourcemanager_v3_projects_client = resourcemanager_v3.ProjectsClient(credentials=credentials)
10
11
# Initialize request argument(s)
12
list_projects_request = resourcemanager_v3.ListProjectsRequest(show_deleted=False, parent='')
13
14
# Make the request
15
page_result = resourcemanager_v3_projects_client.list_projects(request=list_projects_request)
16
17
18
# -> Error...
19
# -> grpc_helpers.py", line 68, in error_remapped_callable
20
# -> raise exceptions.from_grpc_error(exc) from exc
21
# -> google.api_core.exceptions.PermissionDenied: 403 The caller does not have permission
22
Does the Python Client for Google Cloud Resource Manager
needs some other permission than the gcloud cli
or do I miss something inside the Python code?
Advertisement
Answer
Seems I missed the parent
parameter…
The following snipped should list the projects of a specific folder or organization.
JavaScript
1
20
20
1
# pip install google-cloud-resource-manager==1.4.1
2
from google.oauth2 import service_account
3
from google.cloud import resourcemanager_v3
4
5
# Load gcp credentials
6
credentials = service_account.Credentials.from_service_account_file('./key.json')
7
8
# Create resourcemanager_v3 ProjectsClient
9
resourcemanager_v3_projects_client = resourcemanager_v3.ProjectsClient(credentials=credentials)
10
11
# Initialize request argument(s)
12
list_projects_request = resourcemanager_v3.ListProjectsRequest(show_deleted=False, parent='folders/%folder-id%') # for organization: 'organizations/%organization-id%'
13
14
# Make the request
15
page_result = resourcemanager_v3_projects_client.list_projects(request=list_projects_request)
16
17
# Handle the response
18
for response in page_result:
19
print(response)
20