使用Python调用GCP vulnerabilityReports.get()获取实例漏洞报告时遇TypeError参数错误的解决咨询
Fixing "TypeError: Got an unexpected keyword argument 'parent'" in Google Cloud OS Config Vulnerability Report API Call
The Problem
I'm trying to fetch a vulnerability report for a Compute Engine instance using a Python script, following the Google Cloud OS Config API docs. Here's my current function:
def getVulnerability(): project = "my-project" location = "my-zone" instance = "my-instance-id" service.projects().locations().instances().vulnerabilityReports().get( parent='projects/{}/locations/{}/instances/{}'.format(project, location, instance) )
When I run this, I get the error: TypeError: Got an unexpected keyword argument "parent". How can I fix my code to resolve this?
The Solution
Ah, I see the issue here! The vulnerabilityReports().get() method doesn't accept a parent parameter — it requires a name parameter that specifies the full resource path of the vulnerability report you want to retrieve.
What to adjust:
- Swap out the
parentkeyword argument withname. - The
nameparameter must follow this format:projects/{project}/locations/{location}/instances/{instance}/vulnerabilityReports/-(the-at the end is a wildcard that tells the API to fetch the latest vulnerability report for your instance).
Fixed Code Example
def getVulnerability(): project = "my-project" location = "my-zone" instance = "my-instance-id" # Construct the full resource name for the vulnerability report report_name = f'projects/{project}/locations/{location}/instances/{instance}/vulnerabilityReports/-' # Create the API request and execute it request = service.projects().locations().instances().vulnerabilityReports().get( name=report_name ) response = request.execute() # Don't forget this step — it sends the request to Google's API! # Process the response data as needed print("Vulnerability Report:", response)
Important Notes
- Wildcard for latest report: Using
-as the report ID is the standard way to get the most recent vulnerability data for your instance. If you ever need a specific historical report, you can replace-with the actual report ID (though this is rarely needed for most use cases). - Execute the request: Your original code only built the request object but never sent it to the API. Adding
request.execute()is critical to actually retrieve the report data. - Authentication: Double-check that your script is properly authenticated (e.g., via the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable or service account keys) — otherwise, you'll run into authorization errors even after fixing the parameter issue.
内容的提问来源于stack exchange,提问作者Beembo
相关产品推荐
相关产品推荐

