Reputation: 111
How to get the list of all projects in Google Cloud Platform(GCP) using resourcemanager or resourcemanager_v3 python client library?
I am using GCP free trial account so can't create organizations.
To get the list of projects in Google Cloud Platform (GCP) I am using google.cloud.resourcemanager_v3 Python client library.
here is the code sample:-
from google.cloud import resourcemanager_v3
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'My Service Account JSON File'
# Create a client object for interacting with the Resource Manager API.
client = resourcemanager_v3.ProjectsClient()
# Use the client to fetch a list of all the projects that the authenticated user has access to.
projects = client.list_projects()
for project in projects:
print(f"{project.project_id} ({project.name})")
I have also provided the necessary permissions to service account like project owner. But encountering the below error
Traceback (most recent call last): File "/home/jatin/.local/lib/python3.10/site-packages/google/api_core/grpc_helpers.py", line 72, in error_remapped_callable return callable_(*args, **kwargs) File "/home/jatin/.local/lib/python3.10/site-packages/grpc/_channel.py", line 946, in call return _end_unary_response_blocking(state, call, False, None) File "/home/jatin/.local/lib/python3.10/site-packages/grpc/_channel.py", line 849, in _end_unary_response_blocking raise _InactiveRpcError(state) grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with: status = StatusCode.PERMISSION_DENIED details = "The caller does not have permission" debug_error_string = "UNKNOWN:Error received from peer ipv4:142.250.194.202:443 {grpc_message:"The caller does not have permission", grpc_status:7, created_time:"2023-02-20T10:40:54.578602542+05:30"}"
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "/home/jatin/work/official Scripts/test2.py", line 14, in <module> projects = client.list_projects() File "/home/jatin/.local/lib/python3.10/site-packages/google/cloud/resourcemanager_v3/services/projects/client.py", line 659, in list_projects response = rpc( File "/home/jatin/.local/lib/python3.10/site-packages/google/api_core/gapic_v1/method.py", line 113, in call return wrapped_func(*args, **kwargs) File "/home/jatin/.local/lib/python3.10/site-packages/google/api_core/retry.py", line 349, in retry_wrapped_func return retry_target( File "/home/jatin/.local/lib/python3.10/site-packages/google/api_core/retry.py", line 191, in retry_target return target() File "/home/jatin/.local/lib/python3.10/site-packages/google/api_core/timeout.py", line 120, in func_with_timeout return func(*args, **kwargs) File "/home/jatin/.local/lib/python3.10/site-packages/google/api_core/grpc_helpers.py", line 74, in error_remapped_callable raise exceptions.from_grpc_error(exc) from exc google.api_core.exceptions.PermissionDenied: 403 The caller does not have permission
Upvotes: 0
Views: 2288
Reputation: 300
To list projects in Google Cloud Platform(GCP):
You can list projects for which you have permissions to access. This means that you cannot see all projects unless you have the rights to access them.In the article below you can see the examples which scopes are required. This also means that you can list projects across accounts. This allows you to see which projects you have access to using the credentials specified in the examples. It shows how to use Application Default Credentials (ADC) and Service Account Credentials (Json file format).
These examples have been tested with Python 3.6 on Windows 10 Professional. These examples will display the project list exactly as the CLI.
For more information you can read the article by John Hanley about projects. Also you can refer to the Python Client for Resource Manager API.
Error Message : “PermissionDenied: 403 The caller does not have permission”
You can review the following recommendations depending on how you authenticate the access to Google Cloud:
Using a Service account. You need to generate its credentials in a JSON file, then you have to set up the environment variable GOOGLE_APPLICATION_CREDENTIALS. An easy way to do it from your code is: os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path/to/imgtotext.json"
Upvotes: 0