谷歌安全中心Python API失败

2024-04-23 22:24:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python API从Google安全中心检索结果。我已经安装了python库,设置了一个服务帐户,生成了一个密钥,当我尝试获取查找或任何客户端函数时,我得到了以下错误:

Traceback (most recent call last):
  File "./find.py", line 12, in <module>
    finding_result_iterator = client.list_findings(all_sources)
  File "/usr/local/lib/python3.6/site-packages/google/cloud/securitycenter_v1/gapic/security_center_client.py", line 1532, in list_findings
    self.transport.list_findings,
AttributeError: 'str' object has no attribute 'list_findings'

我正在使用下面的代码示例: https://cloud.google.com/security-command-center/docs/how-to-api-list-findings

使用Python3.6,我在客户端创建中有json密钥文件和我的组织id。你知道为什么我不能让任何客户端函数工作吗


Tags: 函数inpyclientapicloud客户端google
1条回答
网友
1楼 · 发布于 2024-04-23 22:24:10

I am passing the API Key in as the argument for authentication like this

client = securitycenter.SecurityCenterClient("gcp-sc.json")

如果您有一个名为gcp-sc.json的文件,其中包含Google凭证数据,则

  • 将环境变量GOOGLE_APPLICATION_CREDENTIALS设置为指向该路径,然后在没有配置的情况下初始化客户机(SecurityCenterClient()),它将拾取该路径
  • 或者,如果您需要显式地命名文件,SecurityCenterClient.from_service_account_json('gcp-sc.json')应该这样做

还可以将自定义凭据对象(请参见the docs)作为SecurityCenterClient(credentials=...)传入

相关问题 更多 >