Google-Cloud-Resource-Manage列表项目403呼叫者未经许可

发布于 2025-01-22 11:04:22 字数 1495 浏览 0 评论 0原文

我创建了一个GCP服务帐户,并为列出组织内部项目的所需权限。当我使用gcloud cli时,一切都起作用:

gcloud auth activate-service-account --key-file=./key.json
gcloud projects list

# -> List of all projects

但是,当我尝试使用 Google Cloud Resource Manager的Python客户端,我收到403呼叫者没有权限错误消息。

# pip install google-cloud-resource-manager==1.4.1
from google.oauth2 import service_account
from google.cloud import resourcemanager_v3

# Load gcp credentials
credentials = service_account.Credentials.from_service_account_file('./key.json')

# Create resourcemanager_v3 ProjectsClient
resourcemanager_v3_projects_client = resourcemanager_v3.ProjectsClient(credentials=credentials)

# Initialize request argument(s)
list_projects_request = resourcemanager_v3.ListProjectsRequest(show_deleted=False, parent='')

# Make the request
page_result = resourcemanager_v3_projects_client.list_projects(request=list_projects_request)


# -> Error...
# -> grpc_helpers.py", line 68, in error_remapped_callable
# -> raise exceptions.from_grpc_error(exc) from exc
# -> google.api_core.exceptions.PermissionDenied: 403 The caller does not have permission

Google Cloud Resource Manager的Python客户端是否需要gcloud cli>的其他权限,还是我错过了python代码中的某些内容?

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:

gcloud auth activate-service-account --key-file=./key.json
gcloud projects list

# -> List of all projects

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.

# pip install google-cloud-resource-manager==1.4.1
from google.oauth2 import service_account
from google.cloud import resourcemanager_v3

# Load gcp credentials
credentials = service_account.Credentials.from_service_account_file('./key.json')

# Create resourcemanager_v3 ProjectsClient
resourcemanager_v3_projects_client = resourcemanager_v3.ProjectsClient(credentials=credentials)

# Initialize request argument(s)
list_projects_request = resourcemanager_v3.ListProjectsRequest(show_deleted=False, parent='')

# Make the request
page_result = resourcemanager_v3_projects_client.list_projects(request=list_projects_request)


# -> Error...
# -> grpc_helpers.py", line 68, in error_remapped_callable
# -> raise exceptions.from_grpc_error(exc) from exc
# -> google.api_core.exceptions.PermissionDenied: 403 The caller does not have permission

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2025-01-29 11:04:22

Seems I missed the parent

# pip install google-cloud-resource-manager==1.4.1
from google.oauth2 import service_account
from google.cloud import resourcemanager_v3

# Load gcp credentials
credentials = service_account.Credentials.from_service_account_file('./key.json')

# Create resourcemanager_v3 ProjectsClient
resourcemanager_v3_projects_client = resourcemanager_v3.ProjectsClient(credentials=credentials)

# Initialize request argument(s)
list_projects_request = resourcemanager_v3.ListProjectsRequest(show_deleted=False, parent='folders/%folder-id%') # for organization: 'organizations/%organization-id%'

# Make the request
page_result = resourcemanager_v3_projects_client.list_projects(request=list_projects_request)

# Handle the response
for response in page_result:
    print(response)

Seems I missed the parent parameter...
The following snipped should list the projects of a specific folder or organization.

# pip install google-cloud-resource-manager==1.4.1
from google.oauth2 import service_account
from google.cloud import resourcemanager_v3

# Load gcp credentials
credentials = service_account.Credentials.from_service_account_file('./key.json')

# Create resourcemanager_v3 ProjectsClient
resourcemanager_v3_projects_client = resourcemanager_v3.ProjectsClient(credentials=credentials)

# Initialize request argument(s)
list_projects_request = resourcemanager_v3.ListProjectsRequest(show_deleted=False, parent='folders/%folder-id%') # for organization: 'organizations/%organization-id%'

# Make the request
page_result = resourcemanager_v3_projects_client.list_projects(request=list_projects_request)

# Handle the response
for response in page_result:
    print(response)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文