首页 > 解决方案 > python脚本中的ListRecommendationsRequest构造函数错误

问题描述

我正在尝试使用 python3.7 脚本准备 VM Resizing Recommendations Report 我的代码如下:

导入日期时间

import logging
from google.cloud import bigquery
from google.cloud import recommender
from google.cloud.exceptions import NotFound
from googleapiclient import discovery

def main(event, context):
 client = recommender.RecommenderClient()
 recommender_type = "google.compute.instance.MachineTypeRecommender"
 projects=list_projects() #This gives list of projects
 # I hard-code the zones below:
 zones = ["us-east1-b","us-east1-c","us-east1-d","us-east4-c","us-east4-b","us-east4-a"
        ,"us-central1-c","us-central1-a","us-central1-f","us-central1-b"
        ,"us-west1-b","us-west1-c","us-west1-a"]

 for zone in zones:
            parent = client.recommender_path("my-project", zone, recommender_type)
            for element in client.list_recommendations(parent): #In this line I am getting this error and these are logs

****Parent****   projects/my-project/locations/us-east1-b/recommenders/google.compute.instance.MachineTypeRecommender
Traceback (most recent call last):
  File "main.py", line 187, in main
    x=list(client.list_recommendations(parent))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/google/cloud/recommender_v1/services/recommender/client.py", line 734, in list_recommendations
    request = recommender_service.ListRecommendationsRequest(request)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/proto/message.py", line 441, in __init__
    raise TypeError(
TypeError: Invalid constructor input for ListRecommendationsRequest: 'projects/my-project/locations/us-east1-b/recommenders/google.compute.instance.MachineTypeRecommender'

During handling of the above exception, another exception occurred:

projects/my-project/locations/us-east1-b/recommenders/google.compute.instance.MachineTypeRecommender是传递给 list_recommendations 函数的参数。我不确定构造函数有什么问题,因为我得到这个:ListRecommendationsRequest 的构造函数输入无效

我是这个 google api 的新手。有人可以帮忙吗?谢谢。

标签: pythongoogle-cloud-platformpython-requestsgoogle-cloud-functionsgoogle-api-python-client

解决方案


我已经检查了我的代码,调用该方法时似乎是语法错误。根据库文档,第一个参数应该是请求对象None并且父参数必须按名称传递。如下更改行我没有错误:

client.list_recommendations(parent=parent)

推荐阅读